Skip to content

Commit

Permalink
build: a couple of tweaks and fixes to yarn admin create.
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
alan-agius4 authored and clydin committed Mar 21, 2024
1 parent 7b2a213 commit 817d99d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 32 deletions.
15 changes: 8 additions & 7 deletions scripts/build.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import { spawn } from 'node:child_process';
import { COPYFILE_FICLONE } from 'node:constants';
import fs from 'node:fs';
import path, { dirname, join, relative, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
Expand All @@ -32,8 +33,7 @@ function _copy(from: string, to: string) {
from = relative(process.cwd(), from);
to = relative(process.cwd(), to);

const buffer = fs.readFileSync(from);
fs.writeFileSync(to, buffer);
fs.copyFileSync(from, to, COPYFILE_FICLONE);
}

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

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

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

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

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

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

logger.info(packageName);

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

output.push({ name: packageDir, outputPath: destDir });
output.push({ name: packageDir, outputPath: destDir, tarPath });
}

logger.groupEnd();
Expand Down
68 changes: 43 additions & 25 deletions scripts/create.mts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
* found in the LICENSE file at https://angular.io/license
*/

import assert from 'assert';
import * as child_process from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import { fileURLToPath, pathToFileURL } from 'url';
import assert from 'node:assert';
import * as child_process from 'node:child_process';
import { copyFile, readFile, rm, writeFile } from 'node:fs/promises';
import * as path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import build from './build.mjs';
import { packages } from './packages.mjs';

export interface CreateOptions {
export interface CreateOptions extends Record<string, unknown> {
_: string[];
}

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

export default async function (args: CreateOptions, cwd: string): Promise<number> {
const projectName = args._[0];
const { _, ...otherArgOptions } = args;
const projectName = _[0];
assert(projectName, 'Project name must be provided.');

const ngNewAdditionalOptions = Object.entries(otherArgOptions).map(
([key, value]) => `--${key}=${value}`,
);

const oldCwd = process.cwd();
console.info('Building...');
await build({ local: true });

const buildResult = await build({ local: true });
const cliBuild = buildResult.find(({ name }) => name === 'angular/cli');

assert(cliBuild);

process.chdir(cwd);
console.info('Creating project...');

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

await _exec(
'npx',
[
'--yes',
pathToFileURL(path.join(__dirname, '../dist/_angular_cli.tgz')).toString(),
'new',
projectName,
'--skip-install',
'--skip-git',
'--no-interactive',
],
{ cwd },
);
console.info('Creating project...');

try {
await _exec(
'npx',
[
'--yes',
pathToFileURL(newTarballName).toString(),
'new',
projectName,
'--skip-install',
'--skip-git',
'--no-interactive',
...ngNewAdditionalOptions,
],
{ cwd },
);
} finally {
await rm(newTarballName, { maxRetries: 3 });
}

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

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

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

console.info('Installing npm packages...');
await _exec('npm', ['install'], { cwd: path.join(cwd, projectName) });
Expand Down
3 changes: 3 additions & 0 deletions scripts/devkit-admin.mts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import yargsParser from 'yargs-parser';

const args = yargsParser(process.argv.slice(2), {
boolean: ['verbose'],
configuration: {
'camel-case-expansion': false,
},
});
const scriptName = args._.shift();

Expand Down

0 comments on commit 817d99d

Please sign in to comment.