Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Directory list workspaces #446

Merged
merged 8 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
895 changes: 505 additions & 390 deletions dist/jsenv_core.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jsenv/core",
"version": "39.7.7",
"version": "39.8.0",
"description": "Tool to develop, test and build js projects",
"license": "MIT",
"author": {
Expand Down Expand Up @@ -70,7 +70,7 @@
"@financial-times/polyfill-useragent-normaliser": "1.10.2",
"@jsenv/abort": "4.3.0",
"@jsenv/ast": "6.4.1",
"@jsenv/filesystem": "4.11.0",
"@jsenv/filesystem": "4.12.0",
"@jsenv/humanize": "1.2.8",
"@jsenv/importmap": "1.2.1",
"@jsenv/integrity": "0.0.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/independent/filesystem/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jsenv/filesystem",
"version": "4.11.0",
"version": "4.12.0",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
2 changes: 2 additions & 0 deletions packages/independent/filesystem/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export {
} from "./path_and_url/file_url_validation.js";
export { comparePathnames } from "./path_and_url/compare_pathnames.js";
export { ensureWindowsDriveLetter } from "./path_and_url/ensure_windows_drive_letter.js";
export { getParentDirectoryUrl } from "./path_and_url/get_parent_directory_url.js";
export { findAncestorDirectoryUrl } from "./path_and_url/find_ancestor_directory_url.js";

// list
export { collectDirectoryMatchReport } from "./list/collect_directory_match_report.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getParentDirectoryUrl } from "./get_parent_directory_url.js";

export const findAncestorDirectoryUrl = (url, callback) => {
url = String(url);
while (url !== "file:///") {
if (callback(url)) {
return url;
}
url = getParentDirectoryUrl(url);
}
return null;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const getParentDirectoryUrl = (url) => {
if (url.startsWith("file://")) {
// With node.js new URL('../', 'file:///C:/').href
// returns "file:///C:/" instead of "file:///"
const resource = url.slice("file://".length);
const slashLastIndex = resource.lastIndexOf("/");
if (slashLastIndex === -1) {
return url;
}
const lastCharIndex = resource.length - 1;
if (slashLastIndex === lastCharIndex) {
const slashBeforeLastIndex = resource.lastIndexOf(
"/",
slashLastIndex - 1,
);
if (slashBeforeLastIndex === -1) {
return url;
}
return `file://${resource.slice(0, slashBeforeLastIndex + 1)}`;
}
return `file://${resource.slice(0, slashLastIndex + 1)}`;
}
return new URL(url.endsWith("/") ? "../" : "./", url).href;
};
Original file line number Diff line number Diff line change
@@ -1,21 +1,59 @@
import { urlToFileSystemPath } from "@jsenv/urls";
import { mkdirSync } from "node:fs";
import { mkdirSync, statSync, unlinkSync } from "node:fs";

import { assertAndNormalizeDirectoryUrl } from "../path_and_url/directory_url_validation.js";
import { findAncestorDirectoryUrl } from "../path_and_url/find_ancestor_directory_url.js";
import { readEntryStatSync } from "./stat/read_entry_stat_sync.js";
import { statsToType } from "./stat/stats_to_type.js";

export const writeDirectorySync = (
destination,
{ recursive = true, allowUseless = false } = {},
{ recursive = true, allowUseless = false, force } = {},
) => {
const destinationUrl = assertAndNormalizeDirectoryUrl(destination);
const destinationPath = urlToFileSystemPath(destinationUrl);

const destinationStats = readEntryStatSync(destinationUrl, {
nullIfNotFound: true,
followLink: false,
});
let destinationStats;
try {
destinationStats = readEntryStatSync(destinationUrl, {
nullIfNotFound: true,
followLink: false,
});
} catch (e) {
if (e.code === "ENOTDIR") {
let previousNonDirUrl = destinationUrl;
// we must try all parent directories as long as it fails with ENOTDIR
findAncestorDirectoryUrl(destinationUrl, (ancestorUrl) => {
try {
statSync(new URL(ancestorUrl));
return true;
} catch (e) {
if (e.code === "ENOTDIR") {
previousNonDirUrl = ancestorUrl;
return false;
}
throw e;
}
});
if (force) {
unlinkSync(
new URL(
previousNonDirUrl
// remove trailing slash
.slice(0, -1),
),
);
} else {
throw new Error(
`cannot write directory at ${destinationPath} because there is a file at ${urlToFileSystemPath(
previousNonDirUrl,
)}`,
);
}
} else {
throw e;
}
}

if (destinationStats) {
if (destinationStats.isDirectory()) {
Expand All @@ -24,7 +62,6 @@ export const writeDirectorySync = (
}
throw new Error(`directory already exists at ${destinationPath}`);
}

const destinationType = statsToType(destinationStats);
throw new Error(
`cannot write directory at ${destinationPath} because there is a ${destinationType}`,
Expand Down
24 changes: 16 additions & 8 deletions packages/independent/filesystem/src/read_write/write_file_sync.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import {
mkdirSync,
readFileSync,
writeFileSync as writeFileSyncNode,
} from "node:fs";
import { readFileSync, writeFileSync as writeFileSyncNode } from "node:fs";

import { assertAndNormalizeFileUrl } from "../path_and_url/file_url_validation.js";
import { removeDirectorySync } from "../remove/remove_directory_sync.js";
import { writeDirectorySync } from "./write_directory_sync.js";

export const writeFileSync = (destination, content = "") => {
export const writeFileSync = (destination, content = "", { force } = {}) => {
const destinationUrl = assertAndNormalizeFileUrl(destination);
const destinationUrlObject = new URL(destinationUrl);
if (content && content instanceof URL) {
Expand All @@ -15,8 +13,18 @@ export const writeFileSync = (destination, content = "") => {
try {
writeFileSyncNode(destinationUrlObject, content);
} catch (error) {
if (error.code === "ENOENT") {
mkdirSync(new URL("./", destinationUrlObject), {
if (error.code === "EISDIR") {
// happens when directory existed but got deleted and now it's a file
if (force) {
removeDirectorySync(destinationUrlObject);
writeFileSyncNode(destinationUrlObject, content);
} else {
throw error;
}
}
if (error.code === "ENOENT" || error.code === "ENOTDIR") {
writeDirectorySync(new URL("./", destinationUrlObject), {
force,
recursive: true,
});
writeFileSyncNode(destinationUrlObject, content);
Expand Down
4 changes: 2 additions & 2 deletions packages/independent/https-local/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jsenv/https-local",
"version": "3.2.14",
"version": "3.2.15",
"description": "A programmatic way to generate locally trusted certificates",
"license": "MIT",
"repository": {
Expand Down Expand Up @@ -40,7 +40,7 @@
"hosts:ensure-localhost-mappings": "node ./scripts/hosts/ensure_localhost_mappings.mjs"
},
"dependencies": {
"@jsenv/filesystem": "4.11.0",
"@jsenv/filesystem": "4.12.0",
"@jsenv/humanize": "1.2.8",
"@jsenv/urls": "2.5.4",
"command-exists": "1.2.9",
Expand Down
4 changes: 2 additions & 2 deletions packages/independent/md-up/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jsenv/md-up",
"version": "0.0.13",
"version": "0.0.14",
"license": "MIT",
"private": true,
"repository": {
Expand All @@ -24,7 +24,7 @@
"dependencies": {
"anchor-markdown-header": "0.7.0",
"marked": "15.0.3",
"@jsenv/filesystem": "4.11.0",
"@jsenv/filesystem": "4.12.0",
"@jsenv/urls": "2.5.4",
"@jsenv/ast": "6.4.1"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/independent/snapshot/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jsenv/snapshot",
"version": "2.11.24",
"version": "2.11.25",
"description": "Snapshot testing",
"license": "MIT",
"author": {
Expand Down Expand Up @@ -34,7 +34,7 @@
"@jsenv/ast": "6.4.1",
"@jsenv/exception": "1.1.3",
"@jsenv/humanize": "1.2.8",
"@jsenv/filesystem": "4.11.0",
"@jsenv/filesystem": "4.12.0",
"@jsenv/terminal-recorder": "1.4.23",
"@jsenv/urls": "2.5.4",
"@jsenv/utils": "2.1.2",
Expand Down
6 changes: 3 additions & 3 deletions packages/independent/workflow/file-size-impact/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jsenv/file-size-impact",
"version": "14.3.12",
"version": "14.3.13",
"description": "Add files size impact into pull requests",
"license": "MIT",
"repository": {
Expand Down Expand Up @@ -34,8 +34,8 @@
},
"dependencies": {
"@jsenv/dynamic-import-worker": "1.2.1",
"@jsenv/filesystem": "4.11.0",
"@jsenv/github-pull-request-impact": "1.8.12",
"@jsenv/filesystem": "4.12.0",
"@jsenv/github-pull-request-impact": "1.8.13",
"@jsenv/urls": "2.5.4",
"@jsenv/humanize": "1.2.8"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jsenv/github-pull-request-impact",
"version": "1.8.12",
"version": "1.8.13",
"description": "Report pull request impact on something",
"license": "MIT",
"repository": {
Expand All @@ -26,7 +26,7 @@
"/src/"
],
"dependencies": {
"@jsenv/filesystem": "4.11.0",
"@jsenv/filesystem": "4.12.0",
"@jsenv/humanize": "1.2.8"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jsenv/github-release-package",
"version": "1.6.12",
"version": "1.6.13",
"description": "Create github release when package version changes.",
"license": "MIT",
"repository": {
Expand All @@ -25,8 +25,8 @@
"/src/"
],
"dependencies": {
"@jsenv/filesystem": "4.11.0",
"@jsenv/filesystem": "4.12.0",
"@jsenv/humanize": "1.2.8",
"@jsenv/github-pull-request-impact": "1.8.12"
"@jsenv/github-pull-request-impact": "1.8.13"
}
}
6 changes: 3 additions & 3 deletions packages/independent/workflow/lighthouse-impact/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jsenv/lighthouse-impact",
"version": "4.2.13",
"version": "4.2.14",
"description": "Package description",
"license": "MIT",
"repository": {
Expand Down Expand Up @@ -31,8 +31,8 @@
"dependencies": {
"@jsenv/abort": "4.3.0",
"@jsenv/dynamic-import-worker": "1.2.1",
"@jsenv/filesystem": "4.11.0",
"@jsenv/github-pull-request-impact": "1.8.12",
"@jsenv/filesystem": "4.12.0",
"@jsenv/github-pull-request-impact": "1.8.13",
"@jsenv/humanize": "1.2.8",
"lighthouse": "12.2.2"
}
Expand Down
6 changes: 3 additions & 3 deletions packages/independent/workflow/monorepo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jsenv/monorepo",
"version": "0.2.3",
"version": "0.2.4",
"description": "Helpers to manage packages in a monorepo",
"license": "MIT",
"repository": {
Expand All @@ -27,8 +27,8 @@
],
"dependencies": {
"@jsenv/urls": "2.5.4",
"@jsenv/filesystem": "4.11.0",
"@jsenv/package-publish": "1.11.12",
"@jsenv/filesystem": "4.12.0",
"@jsenv/package-publish": "1.11.13",
"@jsenv/humanize": "1.2.8",
"semver": "7.6.3"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/independent/workflow/package-publish/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jsenv/package-publish",
"version": "1.11.12",
"version": "1.11.13",
"description": "Publish package to one or many registry.",
"license": "MIT",
"repository": {
Expand All @@ -26,7 +26,7 @@
"/src/"
],
"dependencies": {
"@jsenv/filesystem": "4.11.0",
"@jsenv/filesystem": "4.12.0",
"@jsenv/humanize": "1.2.8",
"semver": "7.6.3"
}
Expand Down
6 changes: 3 additions & 3 deletions packages/independent/workflow/performance-impact/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jsenv/performance-impact",
"version": "4.4.12",
"version": "4.4.13",
"description": "Report pull request impacts on performance metrics",
"license": "MIT",
"repository": {
Expand Down Expand Up @@ -30,8 +30,8 @@
},
"dependencies": {
"@jsenv/dynamic-import-worker": "1.2.1",
"@jsenv/filesystem": "4.11.0",
"@jsenv/github-pull-request-impact": "1.8.12",
"@jsenv/filesystem": "4.12.0",
"@jsenv/github-pull-request-impact": "1.8.13",
"@jsenv/humanize": "1.2.8"
}
}
2 changes: 1 addition & 1 deletion packages/related/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jsenv/cli",
"version": "0.2.25",
"version": "0.2.27",
"description": "Command Line Interface for jsenv",
"license": "MIT",
"repository": {
Expand Down
4 changes: 2 additions & 2 deletions packages/related/cli/template-node-package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
},
"devDependencies": {
"@jsenv/assert": "4.4.2",
"@jsenv/core": "39.7.7",
"@jsenv/core": "39.8.0",
"@jsenv/eslint-config-relax": "1.3.6",
"@jsenv/test": "3.5.31",
"@jsenv/test": "3.5.32",
"eslint": "9.16.0",
"prettier": "3.4.2"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/related/cli/template-web-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
"@babel/plugin-syntax-import-attributes": "7.26.0",
"@jsenv/custom-elements-redefine": "0.0.1",
"@jsenv/assert": "4.4.2",
"@jsenv/core": "39.7.7",
"@jsenv/core": "39.8.0",
"@jsenv/plugin-bundling": "2.7.20",
"@jsenv/plugin-minification": "1.5.12",
"@jsenv/eslint-config-relax": "1.3.6",
"@jsenv/test": "3.5.31",
"@jsenv/test": "3.5.32",
"eslint": "9.16.0",
"open": "10.1.0",
"@playwright/browser-chromium": "1.49.1",
Expand Down
Loading
Loading