-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: removed commander implementation
- Loading branch information
1 parent
f1bd0ca
commit 7bb10d2
Showing
2 changed files
with
23 additions
and
96 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,99 +1,32 @@ | ||
#!/usr/bin/env node | ||
const chalk = require('chalk'); | ||
const { program, Option } = require('commander'); | ||
const themeCommand = require('../lib/install-theme'); | ||
const buildTokensCommand = require('../lib/build-tokens'); | ||
const replaceVariablesCommand = require('../lib/replace-variables'); | ||
const buildScssCommand = require('../lib/build-scss'); | ||
// command: executor function | ||
const COMMANDS = { | ||
'install-theme': themeCommand, | ||
'build-tokens': buildTokensCommand, | ||
'replace-variables': replaceVariablesCommand, | ||
'build-scss': buildScssCommand, | ||
}; | ||
|
||
(async () => { | ||
const [command, ...commandArgs] = process.argv.slice(2); | ||
const executor = COMMANDS[command]; | ||
|
||
if (!executor) { | ||
// eslint-disable-next-line no-console | ||
console.log(chalk.red.bold('Unknown command. Usage: paragon <command>')); | ||
return; | ||
} | ||
|
||
const executor = async (func) => { | ||
const params = process.argv.slice(3); | ||
try { | ||
await func(params); | ||
await executor(commandArgs); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error(chalk.red.bold('An error occurred:', error.message)); | ||
process.exit(1); | ||
} | ||
}; | ||
|
||
program | ||
.version('0.0.1') | ||
.command('build-tokens') | ||
.description('Build design tokens for your project') | ||
.option('-s, --source <filePath>', 'Specify the source directory for design tokens') | ||
.option('-b, --build-dir <filePath>', 'Specify the build directory for the generated tokens') | ||
.option('--source-tokens-only', 'Include only source design tokens in the build') | ||
.option('-t, --themes <themes>', 'Specify themes to include in the token build', val => val.split(',')) | ||
.action(() => executor(buildTokensCommand)); | ||
|
||
program | ||
.version('0.0.1') | ||
.command('replace-variables') | ||
.description('CLI to replace SCSS variables usages or definitions to CSS variables and vice versa in .scss files.') | ||
.requiredOption('-p, --filePath <filePath>', 'Path to the file or directory where to replace variables.') | ||
.addOption(new Option('-s, --source <sourcePath>', 'Type of replacement: usage or definition. If set to "definition" the command will only update SCSS variables definitions with CSS variables, if set to "usage" - all occurrences of SCSS variables will we replaced')) | ||
.addOption(new Option('-t, --replacementType <replacementType>', 'Type of replacement: usage or definition. If set to "definition" the command will only update SCSS variables definitions with CSS variables, if set to "usage" - all occurrences of SCSS variables will we replaced') | ||
.choices(['usage', 'definition']) | ||
.default('definition')) | ||
.addOption(new Option('-d, --direction <name>', 'Map direction: css-to-scss or scss-to-css, if replacement type parameter is set to "definition" this has no effect.') | ||
.choices(['scss-to-css', 'css-to-scss']) | ||
.default('scss-to-css')) | ||
.action(() => executor(replaceVariablesCommand)); | ||
|
||
program | ||
.version('0.0.1') | ||
.command('install-theme') | ||
.description('Install an @edx/brand theme') | ||
.action(() => executor(themeCommand)); | ||
|
||
program | ||
.version('0.0.1') | ||
.command('build-scss') | ||
.description('CLI to compile Paragon\'s core and themes\' SCSS into CSS.') | ||
.addOption( | ||
new Option( | ||
'--corePath <corePath>', | ||
'Path to the theme\'s core SCSS file, defaults to Paragon\'s core.scss.', | ||
), | ||
) | ||
.addOption( | ||
new Option( | ||
'--themesPath <themesPath>', | ||
`Path to the directory that contains themes' files. Expects directory to have following structure: | ||
themes/ | ||
light/ | ||
│ ├─ index.css | ||
│ ├─ other_css_files | ||
dark/ | ||
│ ├─ index.css | ||
│ ├─ other_css_files | ||
some_other_custom_theme/ | ||
│ ├─ index.css | ||
│ ├─ other_css_files | ||
... | ||
where index.css has imported all other CSS files in the theme's subdirectory. The script will output | ||
light.css, dark.css and some_other_custom_theme.css files (together with maps and minified versions). | ||
You can provide any amount of themes. Default to paragon's themes. | ||
`, | ||
), | ||
) | ||
.addOption( | ||
new Option( | ||
'--outDir <outDir>', | ||
'Specifies directory where to out resulting CSS files.', | ||
), | ||
) | ||
.addOption( | ||
new Option( | ||
'--defaultThemeVariants <defaultThemeVariants...>', | ||
`Specifies default theme variants. Defaults to a single 'light' theme variant. | ||
You can provide multiple default theme variants by passing multiple values, for | ||
example: \`--defaultThemeVariants light dark\` | ||
`, | ||
), | ||
) | ||
.action(() => executor(buildScssCommand)); | ||
|
||
program.parse(process.argv); | ||
})(); |
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