Skip to content

Commit

Permalink
feat: add first 2 actions
Browse files Browse the repository at this point in the history
  • Loading branch information
favna committed Jul 1, 2021
1 parent 29a5a17 commit a8ca033
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"rtb": "./dist/cli.js"
},
"imports": {
"#commands/*": "./dist/commands/*.js",
"#lib/*": "./dist/lib/*.js",
"#root/*": "./dist/*.js"
},
Expand Down
15 changes: 12 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env node

import { cleanDist } from '#commands/clean-dist';
import { cleanExtraneousTypes } from '#commands/clean-extraneous-types';
import { cliRootDir, indent, packageCwd } from '#lib/constants';
import { logVerboseError, logVerboseInfo } from '#lib/logVerbose';
import { parseOptionsFile } from '#lib/optionsParser';
Expand All @@ -13,9 +15,6 @@ import { URL } from 'url';
const packageFile = new URL('package.json', cliRootDir);
const packageJson = JSON.parse(await readFile(packageFile, 'utf-8'));

/**
* TODO: --config
*/
const command = new Command()
.version(packageJson.version)
.requiredOption('-d, --dist <dist>', 'The dist directory to target')
Expand Down Expand Up @@ -65,3 +64,13 @@ if (!distPathExistsInCwd) {
exitAfterLog: true
});
}

/**
* Clean the dist directory
*/
cleanDist(options);

/**
* Cleans extraneous types from the dist directory
*/
cleanExtraneousTypes(options);
10 changes: 10 additions & 0 deletions src/commands/clean-dist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { rm } from 'fs/promises';
import type { Options } from '#lib/interfaces';

/**
* Removes the `dist` directory ahead of rebuilding code to ensure a clean build
* @param options The options that tell this function where to clean up
*/
export async function cleanDist(options: Options): Promise<void> {
await rm(options.dist, { recursive: true, force: true });
}
46 changes: 46 additions & 0 deletions src/commands/clean-extraneous-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type { Options } from '#lib/interfaces';
import { logVerboseError } from '#lib/logVerbose';
import { opendir, rm } from 'fs/promises';
import { join, sep } from 'path';
import { fileURLToPath, URL } from 'url';

/**
* Scans a given {@link path} applying the {@link cb} filter
* @param path The path to scan
* @param cb The callback to apply to filter files in the {@link path}
*/
async function* scan(path: URL | string, cb: (path: string) => boolean): AsyncGenerator<string> {
const dir = await opendir(typeof path === 'string' ? path : fileURLToPath(path));

for await (const item of dir) {
const file = join(dir.path, item.name);
if (item.isFile()) {
if (cb(file)) yield file;
} else if (item.isDirectory()) {
yield* scan(file, cb);
}
}
}

/**
* Cleans up the extraneous types from the `dist` folder after bundling all the types into the root `index.d.ts`
* @param options The options that tell this function where to clean up
*/
export async function cleanExtraneousTypes(options: Options) {
try {
const regexp = /(?:\.d\.ts(?:\.map)?|\.tsbuildinfo)$/;
const cb = (path: string) => regexp.test(path);

for await (const path of scan(options.dist, cb)) {
if (!path.endsWith(`dist${sep}index.d.ts`)) {
await rm(path);
}
}
} catch (err) {
logVerboseError({
text: ['An error occurred while removing one or more of the extraneous types from the `dist` directory', 'Please remove them manually'],
verbose: options.verbose,
verboseText: ['I was scanning this dist path: ', options.dist.toString(), 'Furthermore, the exact error that occurred is: ', err]
});
}
}
1 change: 1 addition & 0 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"preserveConstEnums": true,
"baseUrl": ".",
"paths": {
"#commands/*": ["commands/*"],
"#lib/*": ["./lib/*"],
"#root/*": ["*"]
}
Expand Down

0 comments on commit a8ca033

Please sign in to comment.