From 19e38f8c2f6d4a5a7632f3244f2f1ee86bb72a03 Mon Sep 17 00:00:00 2001 From: Igor Randjelovic Date: Mon, 23 Nov 2020 16:27:33 +0100 Subject: [PATCH] feat: add micro cli for initializing the config --- packages/webpack5/package.json | 7 +++- packages/webpack5/src/bin/index.ts | 38 +++++++++++++++++++ .../src/plugins/WatchStateLoggerPlugin.ts | 1 + 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 packages/webpack5/src/bin/index.ts diff --git a/packages/webpack5/package.json b/packages/webpack5/package.json index 7cc1050ee..caa0482b0 100644 --- a/packages/webpack5/package.json +++ b/packages/webpack5/package.json @@ -6,18 +6,23 @@ "files": [ "dist" ], + "bin": { + "nativescript-webpack": "dist/bin/index.js" + }, "license": "Apache-2.0", "scripts": { "build": "tsc", "test": "jest", - "prepack": "npm run build && cp -R src/stubs dist/stubs" + "prepack": "npm run build && cp -R src/stubs dist/stubs && chmod +x dist/bin/index.js" }, "dependencies": { "@babel/core": "^7.12.3", "@pmmmwh/react-refresh-webpack-plugin": "^0.4.3", "babel-loader": "^8.2.1", + "chalk": "^4.1.0", "clean-webpack-plugin": "^3.0.0", "cli-highlight": "^2.1.8", + "commander": "^6.2.0", "copy-webpack-plugin": "^6.3.2", "css": "^3.0.0", "css-loader": "^5.0.1", diff --git a/packages/webpack5/src/bin/index.ts b/packages/webpack5/src/bin/index.ts new file mode 100644 index 000000000..9bdcf2261 --- /dev/null +++ b/packages/webpack5/src/bin/index.ts @@ -0,0 +1,38 @@ +#!/user/bin/env node + +import { redBright, green, greenBright } from 'chalk'; +import { program } from 'commander'; +import dedent from 'ts-dedent'; +import path from 'path'; +import fs from 'fs'; + +const defaultConfig = path.resolve( + __dirname, + '../stubs/default.config.stub.js' +); +const tag = `[${green('@nativescript/webpack')}]`; + +function error(message: string) { + console.error(`${tag} ${redBright(dedent(message))}`); +} + +function info(message: string) { + console.info(`${tag} ${greenBright(dedent(message))}`); +} + +program + .command('init') + .description('Initialize a new webpack.config.js in the current directory.') + .action(() => { + const targetPath = path.resolve(process.cwd(), 'webpack.config.js'); + + if (fs.existsSync(targetPath)) { + return error(`File Already Exists: ${targetPath}`); + } + + fs.copyFileSync(defaultConfig, targetPath); + + info('Initialized config.'); + }); + +program.parse(process.argv); diff --git a/packages/webpack5/src/plugins/WatchStateLoggerPlugin.ts b/packages/webpack5/src/plugins/WatchStateLoggerPlugin.ts index 7f1f0dcb9..78c9a19e0 100644 --- a/packages/webpack5/src/plugins/WatchStateLoggerPlugin.ts +++ b/packages/webpack5/src/plugins/WatchStateLoggerPlugin.ts @@ -11,6 +11,7 @@ export enum messages { /** * This little plugin will report the webpack state through the console. * So the {N} CLI can get some idea when compilation completes. + * @deprecated todo: remove soon */ export class WatchStateLoggerPlugin { isRunningWatching: boolean;