Skip to content

Commit

Permalink
feat: copylib cli util
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Jan 30, 2022
1 parent b333c86 commit a849bc3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
41 changes: 41 additions & 0 deletions bin/partytown.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env node

async function run() {
const args = process.argv.slice(2);
const task = args[0];

if (!task) {
panic('Missing partytown task argument. Example command: partytown copylib dest/directory');
}

switch (task) {
case 'copylib': {
await copyLibTask(args);
break;
}
default: {
panic('Unknown partytown task: ' + task);
}
}
}

async function copyLibTask(args) {
try {
const utils = require('../utils/index.cjs');
const destDir = args[1];
const result = await utils.copyLibFiles(destDir);

if (!args.includes('--silent')) {
console.log('Partytown lib copied to: ' + result.dest);
}
} catch (e) {
panic(String(e.message || e));
}
}

function panic(msg) {
console.error('❌ ' + msg);
process.exit(1);
}

run();
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
"require": "./index.cjs"
}
},
"bin": "bin/partytown.cjs",
"files": [
"bin/partytown.cjs",
"index.cjs",
"index.mjs",
"index.d.ts",
Expand Down
7 changes: 7 additions & 0 deletions src/utils/copy-lib-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,17 @@ export function libDirPath() {
* @public
*/
export async function copyLibFiles(destDir: string) {
if (typeof destDir !== 'string' || destDir.length === 0) {
throw new Error('Missing destDir');
}
if (!isAbsolute(destDir)) {
destDir = resolve(process.cwd(), destDir);
}
await copyLibDir(libDirPath(), destDir);
return {
src: libDirPath(),
dest: destDir,
};
}

async function copyLibDir(srcDir: string, destDir: string) {
Expand Down

1 comment on commit a849bc3

@vercel
Copy link

@vercel vercel bot commented on a849bc3 Jan 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.