create-nx-workspace 24.0.5 → 24.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,22 +3,22 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createSandbox = createSandbox;
4
4
  const tslib_1 = require("tslib");
5
5
  const fs_1 = require("fs");
6
- const tmp_1 = require("tmp");
7
6
  const ora_1 = tslib_1.__importDefault(require("ora"));
8
7
  const path_1 = require("path");
9
8
  const package_manager_1 = require("./utils/package-manager");
10
9
  const child_process_utils_1 = require("./utils/child-process-utils");
11
10
  const nx_version_1 = require("./utils/nx/nx-version");
12
11
  const error_utils_1 = require("./utils/error-utils");
12
+ const temp_install_directory_1 = require("./utils/temp-install-directory");
13
13
  /**
14
14
  * Creates a temporary directory and installs Nx in it.
15
15
  * @param packageManager package manager to use
16
16
  * @returns directory where Nx is installed
17
17
  */
18
18
  async function createSandbox(packageManager) {
19
+ const tmpDir = (0, temp_install_directory_1.createTempInstallDirectory)();
19
20
  const installSpinner = (0, ora_1.default)(`Installing dependencies with ${packageManager}`).start();
20
21
  const { install, preInstall } = (0, package_manager_1.getPackageManagerCommand)(packageManager);
21
- const tmpDir = (0, tmp_1.dirSync)().name;
22
22
  try {
23
23
  (0, fs_1.writeFileSync)((0, path_1.join)(tmpDir, 'package.json'), JSON.stringify({
24
24
  dependencies: {
@@ -47,6 +47,7 @@ async function createSandbox(packageManager) {
47
47
  lines.push(`Exit code: ${exitCode}`);
48
48
  }
49
49
  if (logFile) {
50
+ (0, temp_install_directory_1.keepTempInstallDirectory)(tmpDir);
50
51
  lines.push(`Log file: ${logFile}`);
51
52
  }
52
53
  lines.push(`\nPlease verify that "${install}" runs successfully in a temporary directory.`);
@@ -17,6 +17,7 @@ const output_1 = require("./utils/output");
17
17
  const get_third_party_preset_1 = require("./utils/preset/get-third-party-preset");
18
18
  const preset_1 = require("./utils/preset/preset");
19
19
  const download_template_1 = require("./utils/template/download-template");
20
+ const remove_template_ai_agents_1 = require("./utils/template/remove-template-ai-agents");
20
21
  const update_readme_1 = require("./utils/template/update-readme");
21
22
  const child_process_utils_1 = require("./utils/child-process-utils");
22
23
  const package_manager_1 = require("./utils/package-manager");
@@ -73,6 +74,9 @@ async function createWorkspace(preset, options, rawArgs) {
73
74
  if ((0, node_fs_1.existsSync)(npmLockPath)) {
74
75
  (0, node_fs_1.unlinkSync)(npmLockPath);
75
76
  }
77
+ if ((0, remove_template_ai_agents_1.shouldRemoveTemplateAiAgents)(options, rawArgs)) {
78
+ (0, remove_template_ai_agents_1.removeTemplateAiAgents)(directory);
79
+ }
76
80
  // Generate package manager specific files (e.g., .yarnrc.yml for Yarn Berry)
77
81
  (0, package_manager_1.generatePackageManagerFiles)(directory, packageManager);
78
82
  const packageJsonPath = (0, path_1.join)(directory, 'package.json');
@@ -84,7 +84,7 @@ function getPackageManagerCommand(packageManager = detectPackageManager()) {
84
84
  // bun doesn't current support programmatically reading config https://github.com/oven-sh/bun/issues/7140
85
85
  return {
86
86
  install: 'bun install --ignore-scripts',
87
- exec: 'bunx',
87
+ exec: 'bun',
88
88
  globalAdd: 'bun install -g',
89
89
  };
90
90
  }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Creates an empty owner-only directory for a throwaway package install.
3
+ *
4
+ * Package managers and Node read config and modules from every ancestor of
5
+ * the install, so it lives under `~/.nx/tmp`, else under the OS temp dir, and
6
+ * only where that root and all of its ancestors are closed to other users.
7
+ * Throws when neither root qualifies or can be written. The OS does not reap
8
+ * it, so it is removed when the process exits unless `keepTempInstallDirectory`
9
+ * is called.
10
+ *
11
+ * Mirrors `createTempInstallDirectory` in packages/nx/src/utils/package-manager.ts;
12
+ * create-nx-workspace cannot depend on nx.
13
+ */
14
+ export declare function createTempInstallDirectory(): string;
15
+ /**
16
+ * Leaves `dir` in place at exit, for a failed install whose log the user is
17
+ * pointed at. The stale sweep removes it a day later.
18
+ */
19
+ export declare function keepTempInstallDirectory(dir: string): void;
@@ -0,0 +1,229 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createTempInstallDirectory = createTempInstallDirectory;
4
+ exports.keepTempInstallDirectory = keepTempInstallDirectory;
5
+ const fs_1 = require("fs");
6
+ const os_1 = require("os");
7
+ const path_1 = require("path");
8
+ const tmp_1 = require("tmp");
9
+ const error_utils_1 = require("./error-utils");
10
+ /**
11
+ * Creates an empty owner-only directory for a throwaway package install.
12
+ *
13
+ * Package managers and Node read config and modules from every ancestor of
14
+ * the install, so it lives under `~/.nx/tmp`, else under the OS temp dir, and
15
+ * only where that root and all of its ancestors are closed to other users.
16
+ * Throws when neither root qualifies or can be written. The OS does not reap
17
+ * it, so it is removed when the process exits unless `keepTempInstallDirectory`
18
+ * is called.
19
+ *
20
+ * Mirrors `createTempInstallDirectory` in packages/nx/src/utils/package-manager.ts;
21
+ * create-nx-workspace cannot depend on nx.
22
+ */
23
+ function createTempInstallDirectory() {
24
+ const dir = createInPrivateInstallRoot();
25
+ if (tempInstallDirectories.size === 0) {
26
+ process.once('exit', removeTempInstallDirectories);
27
+ }
28
+ tempInstallDirectories.add(dir);
29
+ return dir;
30
+ }
31
+ /**
32
+ * Leaves `dir` in place at exit, for a failed install whose log the user is
33
+ * pointed at. The stale sweep removes it a day later.
34
+ */
35
+ function keepTempInstallDirectory(dir) {
36
+ tempInstallDirectories.delete(dir);
37
+ }
38
+ const tempInstallDirectories = new Set();
39
+ function removeTempInstallDirectories() {
40
+ for (const dir of tempInstallDirectories) {
41
+ try {
42
+ (0, fs_1.rmSync)(dir, { recursive: true, force: true });
43
+ }
44
+ catch { }
45
+ }
46
+ }
47
+ const STALE_INSTALL_DIRECTORY_MS = 24 * 60 * 60 * 1000;
48
+ /**
49
+ * Removes install dirs left behind by processes that never reached their exit
50
+ * handler. A day outlasts any realistic install, so a concurrent process's
51
+ * install survives.
52
+ */
53
+ function removeStaleInstallDirectories(root) {
54
+ let entries;
55
+ try {
56
+ entries = (0, fs_1.readdirSync)(root);
57
+ }
58
+ catch {
59
+ return;
60
+ }
61
+ const cutoff = Date.now() - STALE_INSTALL_DIRECTORY_MS;
62
+ for (const entry of entries) {
63
+ const path = (0, path_1.join)(root, entry);
64
+ try {
65
+ const stats = (0, fs_1.lstatSync)(path);
66
+ if (stats.isDirectory() &&
67
+ stats.mtimeMs < cutoff &&
68
+ (typeof process.getuid !== 'function' || stats.uid === process.getuid())) {
69
+ (0, fs_1.rmSync)(path, { recursive: true, force: true });
70
+ }
71
+ }
72
+ catch { }
73
+ }
74
+ }
75
+ const TMPDIR_INSTALL_ROOT = 'nx-installs';
76
+ /**
77
+ * An existing `~/.nx/tmp` can still refuse writes, for example under an agent
78
+ * sandbox, so a failed create there moves on to the OS temp dir.
79
+ */
80
+ function createInPrivateInstallRoot() {
81
+ let homeRefusal = homeInstallRootRefusal();
82
+ if (typeof homeRefusal !== 'string') {
83
+ const { root } = homeRefusal;
84
+ try {
85
+ return createInstallDirectory(root);
86
+ }
87
+ catch (e) {
88
+ const code = e?.code;
89
+ homeRefusal = `${root} is not writable${code ? ` (${code})` : ''}`;
90
+ }
91
+ }
92
+ const tmp = (0, os_1.tmpdir)();
93
+ const fallbackRoot = (0, path_1.join)(tmp, TMPDIR_INSTALL_ROOT);
94
+ let tmpRefusal = privateTreeRefusal(tmp);
95
+ if (!tmpRefusal) {
96
+ if (isOwnDirClosedToOthers(fallbackRoot)) {
97
+ return createInstallDirectory(fallbackRoot);
98
+ }
99
+ tmpRefusal = `${fallbackRoot} is not a directory owned by you and closed to other users`;
100
+ }
101
+ if (process.env.NX_ALLOW_INSECURE_DIRECTORIES === 'true') {
102
+ console.warn(`NX_ALLOW_INSECURE_DIRECTORIES is set, so Nx is installing packages in ${tmp} although ${tmpRefusal}.`);
103
+ return (0, tmp_1.dirSync)({ tmpdir: tmp, prefix: 'nx-install-' }).name;
104
+ }
105
+ throw new error_utils_1.CnwError('SANDBOX_FAILED', 'Nx cannot install packages because it found no directory closed to other users to install into.\n' +
106
+ ` ~/.nx/tmp: ${homeRefusal}.\n` +
107
+ ` The OS temp directory: ${tmpRefusal}.\n` +
108
+ 'Package managers and Node read config and modules from every parent of the install directory, so Nx will not install where another user could write to it or to any of its parents.\n' +
109
+ 'Point TMPDIR at a directory only you can write to, or set NX_ALLOW_INSECURE_DIRECTORIES=true to install in the OS temp directory anyway.');
110
+ }
111
+ function createInstallDirectory(root) {
112
+ removeStaleInstallDirectories(root);
113
+ return (0, tmp_1.dirSync)({ tmpdir: root }).name;
114
+ }
115
+ function homeInstallRootRefusal() {
116
+ let home;
117
+ try {
118
+ home = (0, os_1.homedir)();
119
+ }
120
+ catch { }
121
+ if (!home || !(0, path_1.isAbsolute)(home) || isAtOrUnderSharedTmp(home)) {
122
+ return 'there is no home directory separate from the shared temp directory';
123
+ }
124
+ const nxDir = (0, path_1.join)(home, '.nx');
125
+ const root = (0, path_1.join)(nxDir, 'tmp');
126
+ for (const dir of [nxDir, root]) {
127
+ if (!isOwnDirClosedToOthers(dir)) {
128
+ return `${dir} is not a directory owned by you and closed to other users`;
129
+ }
130
+ }
131
+ return privateTreeRefusal(root) ?? { root };
132
+ }
133
+ /**
134
+ * Mirrors `privateTreeRefusal` in packages/nx/src/utils/package-manager.ts.
135
+ */
136
+ function privateTreeRefusal(dir) {
137
+ if (typeof process.getuid !== 'function') {
138
+ try {
139
+ if ((0, fs_1.lstatSync)(dir).isDirectory()) {
140
+ return undefined;
141
+ }
142
+ }
143
+ catch { }
144
+ return `${dir} is not a real directory`;
145
+ }
146
+ for (const leaf of [(0, path_1.resolve)(dir), canonicalDir(dir)]) {
147
+ for (let p = leaf;; p = (0, path_1.dirname)(p)) {
148
+ const refusal = privatePathRefusal(p, p === leaf);
149
+ if (refusal) {
150
+ return refusal;
151
+ }
152
+ if ((0, path_1.dirname)(p) === p) {
153
+ break;
154
+ }
155
+ }
156
+ }
157
+ return undefined;
158
+ }
159
+ function privatePathRefusal(path, isLeaf) {
160
+ let stats;
161
+ try {
162
+ stats = (0, fs_1.lstatSync)(path);
163
+ }
164
+ catch (e) {
165
+ const code = e?.code;
166
+ return `${path} could not be inspected${code ? ` (${code})` : ''}`;
167
+ }
168
+ const uid = process.getuid();
169
+ const trustedOwner = stats.uid === uid || (!isLeaf && stats.uid === 0);
170
+ if (stats.isSymbolicLink() && !isLeaf) {
171
+ return trustedOwner ? undefined : `${path} is owned by uid ${stats.uid}`;
172
+ }
173
+ if (!stats.isDirectory()) {
174
+ return stats.isSymbolicLink()
175
+ ? `${path} is a symlink, not a real directory`
176
+ : `${path} is not a directory`;
177
+ }
178
+ if (stats.mode & 0o022) {
179
+ const mode = (stats.mode & 0o7777).toString(8).padStart(4, '0');
180
+ return `${path} is writable by other users (mode ${mode})`;
181
+ }
182
+ return trustedOwner ? undefined : `${path} is owned by uid ${stats.uid}`;
183
+ }
184
+ function isAtOrUnderSharedTmp(dir) {
185
+ const sharedTmp = (0, os_1.platform)() === 'win32' ? (0, os_1.tmpdir)() : '/tmp';
186
+ const fromSharedTmp = (0, path_1.relative)(canonicalDir(sharedTmp), canonicalDir(dir));
187
+ return (fromSharedTmp === '' ||
188
+ (fromSharedTmp !== '..' &&
189
+ !fromSharedTmp.startsWith('..' + path_1.sep) &&
190
+ !(0, path_1.isAbsolute)(fromSharedTmp)));
191
+ }
192
+ /** Real path of `dir`, resolving its nearest existing ancestor when it is missing. */
193
+ function canonicalDir(dir) {
194
+ const missing = [];
195
+ let candidate = (0, path_1.resolve)(dir);
196
+ while (true) {
197
+ try {
198
+ return (0, path_1.join)((0, fs_1.realpathSync)(candidate), ...missing);
199
+ }
200
+ catch {
201
+ if ((0, path_1.dirname)(candidate) === candidate) {
202
+ return (0, path_1.resolve)(dir);
203
+ }
204
+ missing.unshift((0, path_1.basename)(candidate));
205
+ candidate = (0, path_1.dirname)(candidate);
206
+ }
207
+ }
208
+ }
209
+ function isOwnDirClosedToOthers(dir) {
210
+ try {
211
+ (0, fs_1.mkdirSync)(dir, { mode: 0o700 });
212
+ }
213
+ catch (e) {
214
+ if (e?.code !== 'EEXIST') {
215
+ return false;
216
+ }
217
+ }
218
+ try {
219
+ const stats = (0, fs_1.lstatSync)(dir);
220
+ if (!stats.isDirectory()) {
221
+ return false;
222
+ }
223
+ return (typeof process.getuid !== 'function' ||
224
+ (stats.uid === process.getuid() && !(stats.mode & 0o022)));
225
+ }
226
+ catch {
227
+ return false;
228
+ }
229
+ }
@@ -0,0 +1,6 @@
1
+ export declare function shouldRemoveTemplateAiAgents(options: {
2
+ aiAgents?: readonly string[];
3
+ }, rawArgs?: {
4
+ aiAgents?: readonly string[];
5
+ }): boolean;
6
+ export declare function removeTemplateAiAgents(directory: string): void;
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.shouldRemoveTemplateAiAgents = shouldRemoveTemplateAiAgents;
4
+ exports.removeTemplateAiAgents = removeTemplateAiAgents;
5
+ const node_fs_1 = require("node:fs");
6
+ const node_path_1 = require("node:path");
7
+ const AGENT_DIRECTORIES = [
8
+ '.agents',
9
+ '.claude',
10
+ '.codex',
11
+ '.cursor',
12
+ '.gemini',
13
+ '.opencode',
14
+ ];
15
+ // Copilot files sit beside CI workflows under `.github`. Drop the agent
16
+ // directories and leave `workflows/` in place.
17
+ const GITHUB_AGENT_DIRECTORIES = [
18
+ '.github/agents',
19
+ '.github/prompts',
20
+ '.github/skills',
21
+ '.github/instructions',
22
+ ];
23
+ const AGENT_FILES = ['CLAUDE.md', 'GEMINI.md', 'opencode.json'];
24
+ const GITIGNORE_ENTRIES = new Set([
25
+ '.cursor/rules/nx-rules.mdc',
26
+ '.github/instructions/nx.instructions.md',
27
+ '.claude/worktrees',
28
+ '.claude/settings.local.json',
29
+ ]);
30
+ const NX_RULES_START = '<!-- nx configuration start-->';
31
+ const NX_RULES_END = '<!-- nx configuration end-->';
32
+ // Normalized options.aiAgents is empty for both `--aiAgents=none` and an
33
+ // omitted flag. Only an explicit `none` removes files the template ships.
34
+ function shouldRemoveTemplateAiAgents(options, rawArgs) {
35
+ if (rawArgs) {
36
+ const requested = rawArgs.aiAgents;
37
+ return (!!requested &&
38
+ requested.length > 0 &&
39
+ requested.every((agent) => agent === 'none'));
40
+ }
41
+ return !!options.aiAgents && options.aiAgents.length === 0;
42
+ }
43
+ function removeTemplateAiAgents(directory) {
44
+ for (const relativePath of [
45
+ ...AGENT_DIRECTORIES,
46
+ ...GITHUB_AGENT_DIRECTORIES,
47
+ ]) {
48
+ (0, node_fs_1.rmSync)((0, node_path_1.join)(directory, relativePath), { recursive: true, force: true });
49
+ }
50
+ removeIfEmpty((0, node_path_1.join)(directory, '.github'));
51
+ for (const relativePath of AGENT_FILES) {
52
+ (0, node_fs_1.rmSync)((0, node_path_1.join)(directory, relativePath), { force: true });
53
+ }
54
+ stripNxConfiguration((0, node_path_1.join)(directory, 'AGENTS.md'));
55
+ stripGitignoreEntries((0, node_path_1.join)(directory, '.gitignore'));
56
+ }
57
+ function removeIfEmpty(directory) {
58
+ if (!(0, node_fs_1.existsSync)(directory) || !(0, node_fs_1.statSync)(directory).isDirectory())
59
+ return;
60
+ if ((0, node_fs_1.readdirSync)(directory).length === 0) {
61
+ (0, node_fs_1.rmSync)(directory, { recursive: true, force: true });
62
+ }
63
+ }
64
+ function stripNxConfiguration(path) {
65
+ if (!(0, node_fs_1.existsSync)(path))
66
+ return;
67
+ const original = (0, node_fs_1.readFileSync)(path, 'utf-8');
68
+ if (!original.includes(NX_RULES_START))
69
+ return;
70
+ const nxRulesRegex = new RegExp(`${NX_RULES_START}[\\s\\S]*?${NX_RULES_END}`, 'g');
71
+ const updated = original
72
+ .replace(nxRulesRegex, '')
73
+ .replace(/\n{3,}/g, '\n\n')
74
+ .trim();
75
+ if (!updated) {
76
+ (0, node_fs_1.rmSync)(path, { force: true });
77
+ return;
78
+ }
79
+ (0, node_fs_1.writeFileSync)(path, updated + '\n');
80
+ }
81
+ function stripGitignoreEntries(path) {
82
+ if (!(0, node_fs_1.existsSync)(path))
83
+ return;
84
+ const original = (0, node_fs_1.readFileSync)(path, 'utf-8');
85
+ const endsWithNewline = original.endsWith('\n');
86
+ const filtered = original
87
+ .split('\n')
88
+ .filter((line) => !GITIGNORE_ENTRIES.has(line.trim()));
89
+ let next = filtered.join('\n').replace(/\n{3,}/g, '\n\n');
90
+ if (endsWithNewline && !next.endsWith('\n')) {
91
+ next += '\n';
92
+ }
93
+ if (next !== original) {
94
+ (0, node_fs_1.writeFileSync)(path, next);
95
+ }
96
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-nx-workspace",
3
- "version": "24.0.5",
3
+ "version": "24.0.7",
4
4
  "private": false,
5
5
  "description": "Smart Monorepos · Fast Builds",
6
6
  "repository": {