Skip to content

Commit

Permalink
feat(app-builder): finalize clean, copy and zip scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Feb 22, 2023
1 parent 3f850c6 commit e9be04f
Show file tree
Hide file tree
Showing 23 changed files with 538 additions and 149 deletions.
2 changes: 2 additions & 0 deletions apps/app-builder/bin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
import('../dist/main.js');
13 changes: 11 additions & 2 deletions apps/app-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
"node": "./src/index.ts"
}
},
"types": "./dist/index.d.ts",
"bin": {
"pdk-builder": "./dist/index.js"
},
"files": [
"dist",
"src"
],
"scripts": {
"build": "run ws:tsup:build \"$(pwd)\"",
"build:dev": "run ws:tsup:build:dev \"$(pwd)\"",
Expand All @@ -20,19 +28,20 @@
},
"devDependencies": {
"@myparcel-pdk/build-tsup": "workspace:*",
"@types/adm-zip": "^0.5.0",
"@types/archiver": "^5.3.1",
"@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",
"archiver": "^5.3.1",
"chalk": "^5.2.0",
"commander": "^10.0.0",
"debug": "^4.3.4",
"inquirer": "^9.1.4",
"interpret": "^3.1.1",
"liftoff": "^4.0.0",
"supports-color": "^9.3.1",
"tsup": "^6.6.3"
},
"engines": {
Expand Down
30 changes: 30 additions & 0 deletions apps/app-builder/src/commands/clean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {PdkBuilderCommand} from '../types';
import chalk from 'chalk';
import {createDebugger} from '../utils/createDebugger';
import {exists} from '../utils/exists';
import fs from 'fs';
import path from 'path';
import {reportDryRun} from '../utils/reportDryRun';

export const clean: PdkBuilderCommand = async ({env, config, args}) => {
const debug = createDebugger('clean');

if (args.dryRun) reportDryRun(debug, 'No files will be deleted.');

const dist = path.resolve(env.cwd, config.outDir);

const relativePath = path.relative(env.cwd, config.outDir);

if (!(await exists(dist))) {
debug('Dist folder %s does not exist, skipping...', chalk.greenBright(relativePath));
return;
}

debug('Deleting %s folder', chalk.cyan(path.relative(env.cwd, config.outDir)));

if (!args.dryRun) {
await fs.promises.rm(dist, {recursive: true, force: true});
}

debug('Done');
};
45 changes: 45 additions & 0 deletions apps/app-builder/src/commands/compress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {PdkBuilderCommand} from '../types';
import chalk from 'chalk';
import {createArchive} from '../utils/createArchive';
import {createDebugger} from '../utils/createDebugger';
import {exists} from '../utils/exists';
import fs from 'fs';
import path from 'path';
import {reportDryRun} from '../utils/reportDryRun';
import {resolveFileName} from '../utils/resolveFileName';

export const compress: PdkBuilderCommand = async ({env, config, args}) => {
const debug = createDebugger('compress');

if (args.dryRun) reportDryRun(debug, 'No archive will be created.');

debug('Compressing files for platforms %s', chalk.cyanBright(config.platforms.join(', ')));

await Promise.all(
config.platforms.map(async (platform) => {
const archiveFilename = resolveFileName(config.archiveFilename, config, platform);
const platformFolderName = resolveFileName(config.platformFolderName, config, platform);
const platformDistPath = path.resolve(env.cwd, config.outDir, platformFolderName);

if (!(await exists(platformDistPath))) {
throw new Error(`Platform dist folder ${platformDistPath} does not exist. Run the "copy" command first.`);
}

const archivePath = path.resolve(env.cwd, config.outDir, archiveFilename);

if (await exists(archivePath)) {
debug('Removing existing file %s...', chalk.greenBright(path.relative(env.cwd, archivePath)));
await fs.promises.rm(archivePath);
}

const archive = createArchive(archivePath, debug);

debug('Compressing %s...', chalk.greenBright(path.relative(env.cwd, platformDistPath)));
archive.directory(platformDistPath, platformFolderName);

await archive.finalize();
}),
);

debug('Done');
};
54 changes: 32 additions & 22 deletions apps/app-builder/src/commands/copy.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,59 @@
import {PdkBuilderCommand} from '../types';
import {PdkBuilderCommand, Verbosity} from '../types';
import chalk from 'chalk';
import createDebug from 'debug';
import {createDebugger} from '../utils/createDebugger';
import fs from 'fs';
import glob from 'fast-glob';
import path from 'path';

const debug = createDebug('pdk-builder:copy');
import {reportDryRun} from '../utils/reportDryRun';
import {resolveFileName} from '../utils/resolveFileName';

export const copy: PdkBuilderCommand = async ({env, config, args}) => {
debug.enabled = Boolean(args.debug ?? config.debug);
const debug = createDebugger(`copy`);

if (args.dryRun) reportDryRun(debug, 'No files will be copied.');

const files = glob.sync(config.source);

debug(
'Copying files from %s to %s for platforms %s',
'Copying %s files from %s to %s for output "%s"',
chalk.greenBright(files.length),
chalk.yellow(config.source),
chalk.cyan(config.distFolder),
chalk.cyan(path.relative(env.cwd, config.outDir)),
chalk.cyanBright(config.platforms.join(', ')),
);

if (args.dryRun) {
debug('Dry run is enabled, not actually copying files');
}

const files = glob.sync(config.source);

await Promise.all(
config.platforms.map(async (platform) => {
debug('Copying %s files to %s', chalk.cyan(platform), chalk.cyanBright(platform));
const platformFolderPath = `${config.outDir}/${resolveFileName(config.platformFolderName, config, platform)}`;
const relativeDistFolderPath = path.relative(env.cwd, platformFolderPath);

return Promise.all(
debug('Copying files to %s', chalk.greenBright(relativeDistFolderPath));

const promises = await Promise.all(
files.map(async (file) => {
const source = path.resolve(env.cwd, file);
const target = path.resolve(env.cwd, config.distFolder, platform, file);

debug(
'%s -> %s',
chalk.yellow(path.relative(env.cwd, file)),
chalk.cyan(path.relative(env.cwd, [config.distFolder, platform, file].join(path.sep))),
);
const target = path.resolve(env.cwd, platformFolderPath, file);

if (args.verbose >= Verbosity.VERY_VERY_VERBOSE) {
debug(
'%s -> %s',
chalk.yellow(path.relative(env.cwd, file)),
chalk.cyan(path.relative(env.cwd, [platformFolderPath, file].join(path.sep))),
);
}

if (!args.dryRun) {
await fs.promises.mkdir(path.dirname(target), {recursive: true});
await fs.promises.copyFile(source, target);
}
}),
);

debug('Finished copying files to %s', chalk.greenBright(relativeDistFolderPath));

return promises;
}),
);

debug('Done');
};
2 changes: 1 addition & 1 deletion apps/app-builder/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './zip';
export * from './compress';
export * from './copy';
export * from './rename';
11 changes: 4 additions & 7 deletions apps/app-builder/src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
import {CommandArgs, PdkBuilderContext} from '../types';
import createDebug from 'debug';
import {createDebugger} from '../utils/createDebugger';
import fs from 'fs';
import path from 'path';

const debug = createDebug('pdk-builder:commands:init');

const TEMPLATE = `import {PLATFORMS} from '@myparcel/sdk';
import type {PdkBuilderConfig} from '@myparcel-pdk/app-builder/src';
const TEMPLATE = `import type {PdkBuilderConfig} from '@myparcel-pdk/app-builder/src';
const config:configType = {
name: ':name',
description: ':description',
version: ':version',
source: [],
platforms: [PLATFORMS.MYPARCEL_NAME, PLATFORMS.SENDMYPARCEL_NAME],
outputs: ['myparcelnl', 'myparcelbe'],
};
export default config;
`;

export const init = async <A extends CommandArgs>(context: Omit<PdkBuilderContext, 'config'>): Promise<void> => {
debug.enabled = Boolean(context.args.debug);
const debug = createDebugger('init');

const packageJsonPath = `${context.env.cwd}/package.json`;

Expand Down
17 changes: 16 additions & 1 deletion apps/app-builder/src/commands/rename.ts
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
export const rename = async (...args: any[]): Promise<void> => {};
import {PdkBuilderCommand} from '../types';
import chalk from 'chalk';
import {createDebugger} from '../utils/createDebugger';
import {reportDryRun} from '../utils/reportDryRun';

export const rename: PdkBuilderCommand = async ({env, config, args}) => {
const debug = createDebugger('rename');

if (args.dryRun) reportDryRun(debug, 'No files will be renamed.');

debug('Renaming files for platforms %s', chalk.cyanBright(config.platforms.join(', ')));

debug(chalk.red('(Not actually doing anything, this is not implemented yet.)'));

debug('Done');
};
44 changes: 0 additions & 44 deletions apps/app-builder/src/commands/zip.ts

This file was deleted.

6 changes: 2 additions & 4 deletions apps/app-builder/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {start} from './start';
export type {PdkBuilderConfig, PdkBuilderContext} from './types';

export type {PdkBuilderConfig} from './types';

start();
export {start as pdkBuilder} from './start';
3 changes: 3 additions & 0 deletions apps/app-builder/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {start} from './start';

start();
Loading

0 comments on commit e9be04f

Please sign in to comment.