Skip to content

Commit 817d99d

Browse files
alan-agius4clydin
authored andcommitted
build: a couple of tweaks and fixes to yarn admin create.
This update addresses an issue where an older version of the CLI tarball was mistakenly employed in creating a new application via `yarn admin create`. Additionally, `yarn admin create` now accommodates extra options that can be utilized with `ng new`. Example: ``` yarn admin create my-app --ssr ```
1 parent 7b2a213 commit 817d99d

File tree

3 files changed

+54
-32
lines changed

3 files changed

+54
-32
lines changed

scripts/build.mts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import { spawn } from 'node:child_process';
10+
import { COPYFILE_FICLONE } from 'node:constants';
1011
import fs from 'node:fs';
1112
import path, { dirname, join, relative, resolve } from 'node:path';
1213
import { fileURLToPath } from 'node:url';
@@ -32,8 +33,7 @@ function _copy(from: string, to: string) {
3233
from = relative(process.cwd(), from);
3334
to = relative(process.cwd(), to);
3435

35-
const buffer = fs.readFileSync(from);
36-
fs.writeFileSync(to, buffer);
36+
fs.copyFileSync(from, to, COPYFILE_FICLONE);
3737
}
3838

3939
function _recursiveCopy(from: string, to: string, logger: Console) {
@@ -122,7 +122,7 @@ async function _build(logger: Console, mode: BuildMode): Promise<string[]> {
122122

123123
export default async function (
124124
argv: { local?: boolean; snapshot?: boolean } = {},
125-
): Promise<{ name: string; outputPath: string }[]> {
125+
): Promise<{ name: string; outputPath: string; tarPath: string }[]> {
126126
const logger = globalThis.console;
127127

128128
const bazelBin = await _exec(`${bazelCmd} info bazel-bin`, true, logger);
@@ -139,24 +139,25 @@ export default async function (
139139
}
140140

141141
const targets = await _build(logger, buildMode);
142-
const output: { name: string; outputPath: string }[] = [];
142+
const output = [];
143143

144144
logger.group('Moving packages and tars to dist/');
145145

146146
for (const target of targets) {
147147
const packageDir = target.replace(/\/\/packages\/(.*):npm_package_archive/, '$1');
148148
const bazelOutDir = join(bazelBin, 'packages', packageDir, 'npm_package');
149-
const tarPath = `${bazelBin}/packages/${packageDir}/npm_package_archive.tgz`;
149+
const tarPathInBin = `${bazelBin}/packages/${packageDir}/npm_package_archive.tgz`;
150150
const packageJsonPath = `${bazelOutDir}/package.json`;
151151
const packageName = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')).name;
152152
const destDir = `${distRoot}/${packageName}`;
153153

154154
logger.info(packageName);
155155

156156
_recursiveCopy(bazelOutDir, destDir, logger);
157-
_copy(tarPath, `${distRoot}/${packageName.replace('@', '_').replace('/', '_')}.tgz`);
157+
const tarPath = `${distRoot}/${packageName.replace('@', '_').replace('/', '_')}.tgz`;
158+
_copy(tarPathInBin, tarPath);
158159

159-
output.push({ name: packageDir, outputPath: destDir });
160+
output.push({ name: packageDir, outputPath: destDir, tarPath });
160161
}
161162

162163
logger.groupEnd();

scripts/create.mts

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import assert from 'assert';
10-
import * as child_process from 'child_process';
11-
import * as fs from 'fs';
12-
import * as path from 'path';
13-
import { fileURLToPath, pathToFileURL } from 'url';
9+
import assert from 'node:assert';
10+
import * as child_process from 'node:child_process';
11+
import { copyFile, readFile, rm, writeFile } from 'node:fs/promises';
12+
import * as path from 'node:path';
13+
import { fileURLToPath, pathToFileURL } from 'node:url';
1414
import build from './build.mjs';
1515
import { packages } from './packages.mjs';
1616

17-
export interface CreateOptions {
17+
export interface CreateOptions extends Record<string, unknown> {
1818
_: string[];
1919
}
2020

@@ -37,34 +37,52 @@ async function _exec(command: string, args: string[], opts: { cwd?: string }) {
3737
}
3838

3939
export default async function (args: CreateOptions, cwd: string): Promise<number> {
40-
const projectName = args._[0];
40+
const { _, ...otherArgOptions } = args;
41+
const projectName = _[0];
42+
assert(projectName, 'Project name must be provided.');
43+
44+
const ngNewAdditionalOptions = Object.entries(otherArgOptions).map(
45+
([key, value]) => `--${key}=${value}`,
46+
);
4147

4248
const oldCwd = process.cwd();
4349
console.info('Building...');
44-
await build({ local: true });
50+
51+
const buildResult = await build({ local: true });
52+
const cliBuild = buildResult.find(({ name }) => name === 'angular/cli');
53+
54+
assert(cliBuild);
4555

4656
process.chdir(cwd);
47-
console.info('Creating project...');
4857

49-
assert(projectName, 'Project name must be provided.');
58+
// The below is needed as NPX does not guarantee that the updated version is used unless the file name changes.
59+
const newTarballName = cliBuild.tarPath.replace('.tgz', '-' + Date.now() + '.tgz');
60+
await copyFile(cliBuild.tarPath, newTarballName);
5061

51-
await _exec(
52-
'npx',
53-
[
54-
'--yes',
55-
pathToFileURL(path.join(__dirname, '../dist/_angular_cli.tgz')).toString(),
56-
'new',
57-
projectName,
58-
'--skip-install',
59-
'--skip-git',
60-
'--no-interactive',
61-
],
62-
{ cwd },
63-
);
62+
console.info('Creating project...');
63+
64+
try {
65+
await _exec(
66+
'npx',
67+
[
68+
'--yes',
69+
pathToFileURL(newTarballName).toString(),
70+
'new',
71+
projectName,
72+
'--skip-install',
73+
'--skip-git',
74+
'--no-interactive',
75+
...ngNewAdditionalOptions,
76+
],
77+
{ cwd },
78+
);
79+
} finally {
80+
await rm(newTarballName, { maxRetries: 3 });
81+
}
6482

6583
console.info('Updating package.json...');
6684
const packageJsonPath = path.join(projectName, 'package.json');
67-
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
85+
const packageJson = JSON.parse(await readFile(packageJsonPath, 'utf-8'));
6886

6987
if (!packageJson['dependencies']) {
7088
packageJson['dependencies'] = {};
@@ -84,7 +102,7 @@ export default async function (args: CreateOptions, cwd: string): Promise<number
84102
}
85103
}
86104

87-
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), 'utf-8');
105+
await writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2), 'utf-8');
88106

89107
console.info('Installing npm packages...');
90108
await _exec('npm', ['install'], { cwd: path.join(cwd, projectName) });

scripts/devkit-admin.mts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import yargsParser from 'yargs-parser';
1414

1515
const args = yargsParser(process.argv.slice(2), {
1616
boolean: ['verbose'],
17+
configuration: {
18+
'camel-case-expansion': false,
19+
},
1720
});
1821
const scriptName = args._.shift();
1922

0 commit comments

Comments
 (0)