This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
326 additions
and
1,856 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { sync as globby } from 'glob'; | ||
import yargsInteractive, { Option as YargOptions } from 'yargs-interactive'; | ||
import { join, resolve } from 'path'; | ||
import { stripIndent } from 'common-tags'; | ||
import chalk from 'chalk'; | ||
import { readFileSync as readFile, writeFileSync as writeFile } from 'fs'; | ||
import { TranspileOutput, CompilerOptions, transpileModule } from 'typescript'; | ||
import { readFileSync as readJson } from 'jsonfile'; | ||
import { minify as terser } from 'terser'; | ||
|
||
(async () => { | ||
type YargResult = { | ||
help: boolean; | ||
version: boolean; | ||
interactive: boolean; | ||
command: string | string[]; | ||
}; | ||
|
||
type BaseTSConfig = { | ||
compilerOptions: CompilerOptions; | ||
include: string[]; | ||
exclude: string[]; | ||
}; | ||
|
||
const srcDir = join(__dirname, '../src'); | ||
const commandDir = join(srcDir, 'commands'); | ||
const ribbonCommands = globby(`${commandDir}/**/*.ts`).map(file => file.split('/')[6].slice(0, -3)); | ||
const baseTSConfig = readJson(resolve(srcDir, '..', 'tsconfig.json')) as BaseTSConfig; | ||
|
||
const compile = (fileContent: string, options?: CompilerOptions): TranspileOutput => { | ||
const compilerOptions: typeof options = { | ||
...baseTSConfig.compilerOptions, | ||
...options, | ||
}; | ||
|
||
return transpileModule(fileContent, { compilerOptions }); | ||
}; | ||
|
||
try { | ||
const yargOptions: YargOptions = { | ||
interactive: { default: true }, | ||
command: { | ||
type: 'checkbox', | ||
describe: 'Which commands should be reloaded?', | ||
prompt: 'if-empty', | ||
choices: ribbonCommands, | ||
}, | ||
}; | ||
|
||
const results = await yargsInteractive() | ||
.usage((stripIndent` | ||
${chalk.yellow('Ribbon Reloader')} | ||
${chalk.cyan('Usage:')} | ||
${chalk.green('yarn reload')} | ||
${chalk.green('yarn reload')} --command <command> | ||
${chalk.green('yarn reload')} --help` | ||
)) | ||
.interactive(yargOptions) as YargResult; | ||
|
||
const commandsResult: string[] = Array.isArray(results.command) ? results.command : [ results.command ]; | ||
|
||
for (const result of commandsResult) { | ||
const filePath = globby(`${commandDir}/**/${result}.ts`)[0]; | ||
const fileContent = readFile(filePath, { encoding: 'utf8' }); | ||
const transpiledModule = compile(fileContent).outputText; | ||
const minfiedModule = terser(transpiledModule, { compress: true, ecma: 6, mangle: true }).code; | ||
const distPath = filePath.replace(/\/src\//, '/dist/').replace(/\.ts$/, '.js'); | ||
|
||
writeFile(distPath, minfiedModule, { encoding: 'utf8' }); | ||
} | ||
} catch (err) { | ||
throw new Error(err); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.