Skip to content

Commit

Permalink
feat: option to not copy debug dir
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Feb 4, 2022
1 parent 035c26e commit b888df2
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 24 deletions.
49 changes: 31 additions & 18 deletions bin/partytown.cjs
Original file line number Diff line number Diff line change
@@ -1,40 +1,53 @@
#!/usr/bin/env node

async function run() {
const args = process.argv.slice(2);
const task = args[0];
const task = process.argv.slice(2).filter((a) => !a.startsWith('-'))[0];
const args = process.argv.slice(2).filter((a) => a !== task);

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);
}
if (task === 'help' || args.includes('--help') || args.includes('-h')) {
help();
} else if (task === 'version' || args.includes('--version') || args.includes('-v')) {
console.log(version());
} else if (task === 'copylib') {
await copyLibTask(args);
} else {
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);
const destDir = args.filter((a) => !a.startsWith('-'))[0];
const logResult = !args.includes('--silent');
const includeDebugDir = !args.includes('--no-debug');
const result = await utils.copyLibFiles(destDir, {
debugDir: includeDebugDir,
});

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

function help() {
console.log(``);
console.log(`Partytown (${version()}):`);
console.log(``);
console.log(` copylib <destDir> [--no-debug | --silent]`);
console.log(``);
}

function version() {
return require('../package.json').version;
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/react/snippet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface PartytownProps extends PartytownConfig {}

/**
* The React `<Partytown/>` component should be placed within the `<head>`
* of the document`.
* of the document.
*
* @public
*/
Expand Down
7 changes: 6 additions & 1 deletion src/utils/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
```ts

// @public
export function copyLibFiles(destDir: string): Promise<{
export function copyLibFiles(destDir: string, opts?: CopyLibFilesOptions): Promise<{
src: string;
dest: string;
}>;

// @public (undocumented)
export interface CopyLibFilesOptions {
debugDir?: boolean;
}

// @public
export function libDirPath(): string;

Expand Down
25 changes: 21 additions & 4 deletions src/utils/copy-lib-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,27 @@ export function libDirPath() {
* This utility function is to make it easier to locate the source library files
* and copy them to your server's correct location, for example: `./public/~partytown/`.
*
* By default, both the minified and debug builds are copied to the destination.
* However, by setting the `debugDir` option to `false`, the debug directory will
* not be copied.
*
* @public
*/
export async function copyLibFiles(destDir: string) {
export async function copyLibFiles(destDir: string, opts: CopyLibFilesOptions = {}) {
if (typeof destDir !== 'string' || destDir.length === 0) {
throw new Error('Missing destDir');
}
if (!isAbsolute(destDir)) {
destDir = resolve(process.cwd(), destDir);
}
await copyLibDir(libDirPath(), destDir);
await copyLibDir(libDirPath(), destDir, opts);
return {
src: libDirPath(),
dest: destDir,
};
}

async function copyLibDir(srcDir: string, destDir: string) {
async function copyLibDir(srcDir: string, destDir: string, opts: CopyLibFilesOptions) {
try {
await mkdir(destDir, { recursive: true });
} catch (e) {}
Expand All @@ -61,8 +65,21 @@ async function copyLibDir(srcDir: string, destDir: string) {
if (s.isFile()) {
await copyFile(srcPath, destPath);
} else if (s.isDirectory()) {
await copyLibDir(srcPath, destPath);
if (srcName === 'debug' && opts.debugDir === false) {
return;
}
await copyLibDir(srcPath, destPath, opts);
}
})
);
}

/**
* @public
*/
export interface CopyLibFilesOptions {
/**
* When set to `false` the debug directory will not be copied.
*/
debugDir?: boolean;
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { copyLibFiles, libDirPath } from './copy-lib-files';
export type { CopyLibFilesOptions } from './copy-lib-files';

1 comment on commit b888df2

@vercel
Copy link

@vercel vercel bot commented on b888df2 Feb 4, 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.