Skip to content

Commit

Permalink
fix(app-builder): update copy command
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Feb 22, 2023
1 parent 324e13d commit b983220
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 23 deletions.
3 changes: 3 additions & 0 deletions apps/app-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
45 changes: 35 additions & 10 deletions apps/app-builder/src/commands/copy.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}),
);
}),
);
};
45 changes: 44 additions & 1 deletion apps/app-builder/src/commands/zip.ts
Original file line number Diff line number Diff line change
@@ -1 +1,44 @@
export const zip = async (...args: any[]): Promise<void> => {};
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);
}
}),
);
}),
);
};
25 changes: 19 additions & 6 deletions apps/app-builder/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,15 +22,19 @@ 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
.command('all')
.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)]);
Expand All @@ -39,23 +46,29 @@ 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
.command('rename')
.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
.command('zip')
.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);
Expand Down
11 changes: 9 additions & 2 deletions apps/app-builder/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import {LiftoffEnv} from 'liftoff';
import {PlatformName} from '@myparcel/sdk';
import {PromiseOr} from '@myparcel/ts-utils';

export type PdkBuilderConfig = {
name: string;
version: string;
description: string;

/**
* Filename for the final zip file. Defaults to `:platform-:name-:version`.
*/
zipFileName?: string;

/**
* Enable debug logging.
*/
debug?: boolean;

/**
Expand All @@ -17,7 +24,7 @@ export type PdkBuilderConfig = {
/**
* Platforms to include.
*/
platforms: PlatformName[];
platforms: string[];

/**
* Glob patterns to include in final folder.
Expand Down
5 changes: 3 additions & 2 deletions apps/app-builder/src/utils/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ export const createWithContext: CreateHook<WithContextParams> = (env) => {
export const createWithConfig: CreateHook<WithConfigParams, CommandArgs> = (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});
};
};
};
1 change: 1 addition & 0 deletions apps/app-builder/src/utils/mergeDefaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import {PdkBuilderConfig} from '../types';
export const mergeDefaultConfig = (config: PdkBuilderConfig): Required<PdkBuilderConfig> => ({
distFolder: 'dist',
debug: false,
zipFileName: ':platform-:name-:version',
...config,
});
4 changes: 3 additions & 1 deletion apps/app-builder/src/utils/resolveConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ export async function resolveConfig(env: LiftoffEnv): Promise<PdkBuilderConfig>
throw new Error('No config file found.');
}

return (await import(env.configPath)).default;
const imported = await import(env.configPath);

return imported.default.default;
}
7 changes: 7 additions & 0 deletions apps/app-builder/src/utils/resolveFileName.ts
Original file line number Diff line number Diff line change
@@ -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);
10 changes: 10 additions & 0 deletions apps/app-builder/src/utils/spawnPromise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import child_process from 'child_process';

export const spawnPromise = (command: string, args: string[], options: child_process.SpawnOptions): Promise<void> => {
return new Promise((resolve, reject) => {
const child = child_process.spawn(command, args, options);

child.on('error', reject);
child.on('close', resolve);
});
};
21 changes: 20 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b983220

Please sign in to comment.