From b9832202577af80fcac94e9e18446884e110931a Mon Sep 17 00:00:00 2001 From: Edie Lemoine Date: Wed, 22 Feb 2023 13:37:42 +0100 Subject: [PATCH] fix(app-builder): update copy command --- apps/app-builder/package.json | 3 ++ apps/app-builder/src/commands/copy.ts | 45 ++++++++++++++----- apps/app-builder/src/commands/zip.ts | 45 ++++++++++++++++++- apps/app-builder/src/run.ts | 25 ++++++++--- apps/app-builder/src/types.ts | 11 ++++- apps/app-builder/src/utils/hooks.ts | 5 ++- .../src/utils/mergeDefaultConfig.ts | 1 + apps/app-builder/src/utils/resolveConfig.ts | 4 +- apps/app-builder/src/utils/resolveFileName.ts | 7 +++ apps/app-builder/src/utils/spawnPromise.ts | 10 +++++ yarn.lock | 21 ++++++++- 11 files changed, 154 insertions(+), 23 deletions(-) create mode 100644 apps/app-builder/src/utils/resolveFileName.ts create mode 100644 apps/app-builder/src/utils/spawnPromise.ts diff --git a/apps/app-builder/package.json b/apps/app-builder/package.json index 73cdc40e4..81ab35ab1 100644 --- a/apps/app-builder/package.json +++ b/apps/app-builder/package.json @@ -20,11 +20,14 @@ }, "devDependencies": { "@myparcel-pdk/build-tsup": "workspace:*", + "@types/adm-zip": "^0.5.0", "@types/commander": "^2.12.2", "@types/debug": "^4.1.7", "@types/inquirer": "^9.0.3", "@types/interpret": "^1.1.1", "@types/liftoff": "^4.0.0", + "adm-zip": "^0.5.10", + "chalk": "^5.2.0", "commander": "^10.0.0", "debug": "^4.3.4", "inquirer": "^9.1.4", diff --git a/apps/app-builder/src/commands/copy.ts b/apps/app-builder/src/commands/copy.ts index 5ac12bc42..dfe8e36dc 100644 --- a/apps/app-builder/src/commands/copy.ts +++ b/apps/app-builder/src/commands/copy.ts @@ -1,24 +1,49 @@ import {PdkBuilderCommand} from '../types'; -import debug from 'debug'; +import chalk from 'chalk'; +import createDebug from 'debug'; import fs from 'fs'; import glob from 'fast-glob'; import path from 'path'; -const log = debug('pdk:copy'); +const debug = createDebug('pdk-builder:copy'); -export const copy: PdkBuilderCommand = async ({env, config}) => { - const files = glob.sync(config.source); +export const copy: PdkBuilderCommand = async ({env, config, args}) => { + debug.enabled = Boolean(args.debug ?? config.debug); + + debug( + 'Copying files from %s to %s for platforms %s', + chalk.yellow(config.source), + chalk.cyan(config.distFolder), + chalk.cyanBright(config.platforms.join(', ')), + ); - console.log({files, config: config}); + if (args.dryRun) { + debug('Dry run is enabled, not actually copying files'); + } + + const files = glob.sync(config.source); await Promise.all( - files.map(async (file) => { - const source = path.resolve(env.cwd, file); - const target = path.resolve(env.cwd, config.distFolder, file); + config.platforms.map(async (platform) => { + debug('Copying %s files to %s', chalk.cyan(platform), chalk.cyanBright(platform)); + + return Promise.all( + files.map(async (file) => { + const source = path.resolve(env.cwd, file); + const target = path.resolve(env.cwd, config.distFolder, platform, file); - log('Copying %s to %s', source, target); + debug( + '%s -> %s', + chalk.yellow(path.relative(env.cwd, file)), + chalk.cyan(path.relative(env.cwd, [config.distFolder, platform, file].join(path.sep))), + ); - await fs.promises.copyFile(source, target); + if (!args.dryRun) { + await fs.promises.mkdir(path.dirname(target), {recursive: true}); + await fs.promises.copyFile(source, target); + } + }), + ); }), ); }; diff --git a/apps/app-builder/src/commands/zip.ts b/apps/app-builder/src/commands/zip.ts index dfad22808..0532b936a 100644 --- a/apps/app-builder/src/commands/zip.ts +++ b/apps/app-builder/src/commands/zip.ts @@ -1 +1,44 @@ -export const zip = async (...args: any[]): Promise => {}; +import AdmZip from 'adm-zip'; +import {PdkBuilderCommand} from '../types'; +import chalk from 'chalk'; +import createDebug from 'debug'; +import glob from 'fast-glob'; +import path from 'path'; +import {resolveFileName} from '../utils/resolveFileName'; + +const debug = createDebug('pdk-builder:zip'); + +export const zip: PdkBuilderCommand = async ({env, config, args}) => { + debug.enabled = Boolean(args.debug ?? config.debug); + + debug('Zipping files for platforms %s', chalk.cyanBright(config.platforms.join(', '))); + + if (args.dryRun) { + debug('Dry run is enabled, not actually making zips'); + } + + await Promise.all( + config.platforms.map(async (platform) => { + debug('Zipping %s files', chalk.cyan(platform)); + + const zipFile = resolveFileName(config.zipFileName, config, platform); + + return Promise.all( + // eslint-disable-next-line @typescript-eslint/require-await + glob.sync('./**/*', {cwd: path.resolve(config.distFolder, platform)}).map(async (file) => { + const target = path.resolve(env.cwd, config.distFolder, platform, file); + + console.log(target); + + if (!args.dryRun) { + const zip = new AdmZip(); + + zip.addLocalFile(target); + + zip.writeZip(zipFile); + } + }), + ); + }), + ); +}; diff --git a/apps/app-builder/src/run.ts b/apps/app-builder/src/run.ts index 97e3d2b1e..49063c5c8 100644 --- a/apps/app-builder/src/run.ts +++ b/apps/app-builder/src/run.ts @@ -5,7 +5,10 @@ import {init} from './commands/init'; import packageJson from '../package.json' assert {type: 'json'}; import {program} from 'commander'; -const OPTION_DEBUG = ['--debug', 'Enable debug mode'] as const; +const OPTION_VERBOSE = ['-v', 'Enable verbose mode'] as const; +const OPTION_VERY_VERBOSE = ['-vv', 'Enable very verbose mode'] as const; +const OPTION_VERY_VERY_VERBOSE = ['-vvv', 'Enable very very verbose mode'] as const; + const OPTION_DRY_RUN = ['--dry-run', 'Dry run'] as const; const ARGUMENT_PROJECT = ['[project]', 'Project name', 'all'] as const; @@ -19,7 +22,9 @@ export const run = (env: LiftoffEnv, argv: string[]): void => { program .command('init') .description('Create a new config file') - .option(...OPTION_DEBUG) + .option(...OPTION_VERBOSE) + .option(...OPTION_VERY_VERBOSE) + .option(...OPTION_VERY_VERY_VERBOSE) .action(withContext(init)); program @@ -27,7 +32,9 @@ export const run = (env: LiftoffEnv, argv: string[]): void => { .description('Run all commands in sequence') .argument(...ARGUMENT_PROJECT) .option(...OPTION_DRY_RUN) - .option(...OPTION_DEBUG) + .option(...OPTION_VERBOSE) + .option(...OPTION_VERY_VERBOSE) + .option(...OPTION_VERY_VERY_VERBOSE) .action( withConfig(async (context) => { await Promise.all([copy(context), rename(context), zip(context)]); @@ -39,7 +46,9 @@ export const run = (env: LiftoffEnv, argv: string[]): void => { .description('Copy files') .argument(...ARGUMENT_PROJECT) .option(...OPTION_DRY_RUN) - .option(...OPTION_DEBUG) + .option(...OPTION_VERBOSE) + .option(...OPTION_VERY_VERBOSE) + .option(...OPTION_VERY_VERY_VERBOSE) .action(withConfig(copy)); program @@ -47,7 +56,9 @@ export const run = (env: LiftoffEnv, argv: string[]): void => { .description('Rename files') .argument(...ARGUMENT_PROJECT) .option(...OPTION_DRY_RUN) - .option(...OPTION_DEBUG) + .option(...OPTION_VERBOSE) + .option(...OPTION_VERY_VERBOSE) + .option(...OPTION_VERY_VERY_VERBOSE) .action(withConfig(rename)); program @@ -55,7 +66,9 @@ export const run = (env: LiftoffEnv, argv: string[]): void => { .description('Zip dist files') .argument(...ARGUMENT_PROJECT) .option(...OPTION_DRY_RUN) - .option(...OPTION_DEBUG) + .option(...OPTION_VERBOSE) + .option(...OPTION_VERY_VERBOSE) + .option(...OPTION_VERY_VERY_VERBOSE) .action(withConfig(zip)); program.parse(argv); diff --git a/apps/app-builder/src/types.ts b/apps/app-builder/src/types.ts index 95d3fb2a4..8318199ff 100644 --- a/apps/app-builder/src/types.ts +++ b/apps/app-builder/src/types.ts @@ -1,5 +1,4 @@ import {LiftoffEnv} from 'liftoff'; -import {PlatformName} from '@myparcel/sdk'; import {PromiseOr} from '@myparcel/ts-utils'; export type PdkBuilderConfig = { @@ -7,6 +6,14 @@ export type PdkBuilderConfig = { version: string; description: string; + /** + * Filename for the final zip file. Defaults to `:platform-:name-:version`. + */ + zipFileName?: string; + + /** + * Enable debug logging. + */ debug?: boolean; /** @@ -17,7 +24,7 @@ export type PdkBuilderConfig = { /** * Platforms to include. */ - platforms: PlatformName[]; + platforms: string[]; /** * Glob patterns to include in final folder. diff --git a/apps/app-builder/src/utils/hooks.ts b/apps/app-builder/src/utils/hooks.ts index 4e9fa9838..2a686482c 100644 --- a/apps/app-builder/src/utils/hooks.ts +++ b/apps/app-builder/src/utils/hooks.ts @@ -23,8 +23,9 @@ export const createWithContext: CreateHook = (env) => { export const createWithConfig: CreateHook = (env) => { return (callback) => { return async (args) => { - const config = mergeDefaultConfig(await resolveConfig(env)); - return callback({config: config, env, args}); + const config = await resolveConfig(env); + + return callback({config: mergeDefaultConfig(config), env, args}); }; }; }; diff --git a/apps/app-builder/src/utils/mergeDefaultConfig.ts b/apps/app-builder/src/utils/mergeDefaultConfig.ts index b05ef9e1a..14c88145d 100644 --- a/apps/app-builder/src/utils/mergeDefaultConfig.ts +++ b/apps/app-builder/src/utils/mergeDefaultConfig.ts @@ -3,5 +3,6 @@ import {PdkBuilderConfig} from '../types'; export const mergeDefaultConfig = (config: PdkBuilderConfig): Required => ({ distFolder: 'dist', debug: false, + zipFileName: ':platform-:name-:version', ...config, }); diff --git a/apps/app-builder/src/utils/resolveConfig.ts b/apps/app-builder/src/utils/resolveConfig.ts index ea52bcb89..64a29bb53 100644 --- a/apps/app-builder/src/utils/resolveConfig.ts +++ b/apps/app-builder/src/utils/resolveConfig.ts @@ -6,5 +6,7 @@ export async function resolveConfig(env: LiftoffEnv): Promise throw new Error('No config file found.'); } - return (await import(env.configPath)).default; + const imported = await import(env.configPath); + + return imported.default.default; } diff --git a/apps/app-builder/src/utils/resolveFileName.ts b/apps/app-builder/src/utils/resolveFileName.ts new file mode 100644 index 000000000..69dc3584d --- /dev/null +++ b/apps/app-builder/src/utils/resolveFileName.ts @@ -0,0 +1,7 @@ +import {PdkBuilderConfig} from '../types'; + +export const resolveFileName = (filename: string, config: PdkBuilderConfig, platform?: string): string => + filename + .replace(':platform', platform ?? '') + .replace(':name', config.name) + .replace(':version', config.version); diff --git a/apps/app-builder/src/utils/spawnPromise.ts b/apps/app-builder/src/utils/spawnPromise.ts new file mode 100644 index 000000000..f6487b54b --- /dev/null +++ b/apps/app-builder/src/utils/spawnPromise.ts @@ -0,0 +1,10 @@ +import child_process from 'child_process'; + +export const spawnPromise = (command: string, args: string[], options: child_process.SpawnOptions): Promise => { + return new Promise((resolve, reject) => { + const child = child_process.spawn(command, args, options); + + child.on('error', reject); + child.on('close', resolve); + }); +}; diff --git a/yarn.lock b/yarn.lock index d57f63849..f0f642003 100644 --- a/yarn.lock +++ b/yarn.lock @@ -939,11 +939,14 @@ __metadata: resolution: "@myparcel-pdk/app-builder@workspace:apps/app-builder" dependencies: "@myparcel-pdk/build-tsup": "workspace:*" + "@types/adm-zip": ^0.5.0 "@types/commander": ^2.12.2 "@types/debug": ^4.1.7 "@types/inquirer": ^9.0.3 "@types/interpret": ^1.1.1 "@types/liftoff": ^4.0.0 + adm-zip: ^0.5.10 + chalk: ^5.2.0 commander: ^10.0.0 debug: ^4.3.4 inquirer: ^9.1.4 @@ -1552,6 +1555,15 @@ __metadata: languageName: node linkType: hard +"@types/adm-zip@npm:^0.5.0": + version: 0.5.0 + resolution: "@types/adm-zip@npm:0.5.0" + dependencies: + "@types/node": "*" + checksum: 11dd013584e47d431bdf7c115b73cd3162c1a1eca0fbb911f691c9734e904cfe4a01ac1d2d3cbf76d0a952e01fcce8a01fd6fb1c150675a29d740a7fb15325b2 + languageName: node + linkType: hard + "@types/argparse@npm:1.0.38": version: 1.0.38 resolution: "@types/argparse@npm:1.0.38" @@ -2797,6 +2809,13 @@ __metadata: languageName: node linkType: hard +"adm-zip@npm:^0.5.10": + version: 0.5.10 + resolution: "adm-zip@npm:0.5.10" + checksum: 07ed91cf6423bf5dca4ee63977bc7635e91b8d21829c00829d48dce4c6932e1b19e6cfcbe44f1931c956e68795ae97183fc775913883fa48ce88a1ac11fb2034 + languageName: node + linkType: hard + "agent-base@npm:6, agent-base@npm:^6.0.2": version: 6.0.2 resolution: "agent-base@npm:6.0.2" @@ -3442,7 +3461,7 @@ __metadata: languageName: node linkType: hard -"chalk@npm:^5.0.0, chalk@npm:^5.1.2": +"chalk@npm:^5.0.0, chalk@npm:^5.1.2, chalk@npm:^5.2.0": version: 5.2.0 resolution: "chalk@npm:5.2.0" checksum: 03d8060277de6cf2fd567dc25fcf770593eb5bb85f460ce443e49255a30ff1242edd0c90a06a03803b0466ff0687a939b41db1757bec987113e83de89a003caa