Skip to content

Commit

Permalink
add options to choose entry and output directory in webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
DemChing committed Apr 22, 2021
1 parent e678f88 commit cb24ffe
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 94 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Options:
--minify include code minifier plugin for Webpack
--watch Webpack watch changes
--dev-server [port] port of webpack-dev-server
--src <src> entry directory for Webpack
--dist <dist> output directory for Webpack
--typing install typing packages
--no-install do not install packages
--no-clone do not clone static files
Expand All @@ -66,6 +68,8 @@ Options:
| `minify` | include code minifier plugin for Webpack | `false` |
| `watch` | webpack watch changes after build | `false` |
| `dev-server [port]` | set webpack dev server port<br>`9000` if only flag is set | `false` |
| `src <src>` | entry directory for Webpack<br>i.e. entry file located at `./src/index.jsx` | `./src` |
| `dist <dist>` | output directory for Webpack | `./dist` |
| `typing` | install typing packages | `false` |
| `no-install` | do not install packages | `false` |
| `no-clone` | do not clone static files ([file list](#static-files)) | `false` |
Expand Down Expand Up @@ -212,6 +216,8 @@ Options:
--minify include code minifier plugin for Webpack
--watch Webpack watch changes
--dev-server [port] port of webpack-dev-server
--src <src> entry directory for Webpack
--dist <dist> output directory for Webpack
--typing install typing packages
--no-install do not install packages
-h, --help display help for command
Expand Down
12 changes: 10 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ program
.option('--minify', 'include code minifier plugin for Webpack')
.option('--watch', 'Webpack watch changes')
.option('--dev-server [port]', 'port of webpack-dev-server')
.option('--src <src>', 'entry directory for Webpack')
.option('--dist <dist>', 'output directory for Webpack')
.option('--typing', 'install typing packages')
.option('--no-install', 'do not install packages')
.option('--no-clone', 'do not clone static files')
Expand Down Expand Up @@ -153,15 +155,21 @@ program
ts: options.ts,
minify: options.minify,
watch: options.watch,
devServer: options.devServer
devServer: options.devServer,
src: options.src,
dist: options.dist
}, options.force)];
case 6:
_a.sent();
return [4 /*yield*/, package_json_1["default"](cwd, packageJSON, options.force)];
case 7:
_a.sent();
if (!options.clone) return [3 /*break*/, 9];
return [4 /*yield*/, clone_1["default"](cwd, options.ts, options.force)];
return [4 /*yield*/, clone_1["default"](cwd, {
ts: options.ts,
src: options.src,
dist: options.dist
}, options.force)];
case 8:
_a.sent();
_a.label = 9;
Expand Down
34 changes: 26 additions & 8 deletions dist/lib/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ exports.__esModule = true;
var util_1 = require("./util");
var fs_1 = require("fs");
var variables_1 = require("../variables");
function CloneStaticFiles(dir, ts, force) {
function CloneStaticFiles(dir, _a, force) {
var _this = this;
if (ts === void 0) { ts = false; }
var _b = _a.ts, ts = _b === void 0 ? false : _b, _c = _a.src, src = _c === void 0 ? '' : _c, _d = _a.dist, dist = _d === void 0 ? '' : _d;
if (force === void 0) { force = false; }
var msg = "Clone static files";
src = src || variables_1.SrcPath;
dist = dist || variables_1.DistPath;
return util_1.Progress(msg, function () { return new Promise(function (resolve) { return __awaiter(_this, void 0, void 0, function () {
var files, dirs, result, count, readDirectory, directories, filePromise, dirPromise;
var files, dirs, result, count, AppBasePath, AppSrcPath, AppDistPath, FileBasePath, FilesSrcPath, FilesDistPath, replacePath, readDirectory, directories, filePromise, dirPromise;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
Expand All @@ -60,32 +62,48 @@ function CloneStaticFiles(dir, ts, force) {
file: 0,
skip: 0
};
AppBasePath = util_1.PathResolve(dir, '.');
AppSrcPath = util_1.PathResolve(src, dir);
AppDistPath = util_1.PathResolve(dist, dir);
FileBasePath = util_1.PathResolve(variables_1.FilesPath);
FilesSrcPath = util_1.PathResolve(variables_1.SrcPath, FileBasePath);
FilesDistPath = util_1.PathResolve(variables_1.DistPath, FileBasePath);
replacePath = function (from) {
var to = util_1.PathResolve(from);
if (to.indexOf(FilesSrcPath) != -1)
to = to.replace(FilesSrcPath, AppSrcPath);
if (to.indexOf(FilesDistPath) != -1)
to = to.replace(FilesDistPath, AppDistPath);
if (to.indexOf(FileBasePath) != -1)
to = to.replace(FileBasePath, AppBasePath);
return to.replace(AppBasePath, dir).replace(/\\/g, '/');
};
readDirectory = function (path) {
fs_1.readdirSync(util_1.PathResolve(path)).map(function (file) {
var src = path + "/" + file;
if (file.indexOf('.') !== -1) {
if (!/ts/i.test(file) || ts) {
files.push(src.replace(variables_1.FilesPath, ''));
files.push([src, replacePath(src)]);
if (dirs.file.indexOf(path) == -1)
dirs.file.push(path);
}
}
else {
if (dirs.all.indexOf(path) == -1)
dirs.all.push(path);
if (dirs.all.indexOf(src) == -1)
dirs.all.push(src);
readDirectory(src);
}
});
};
readDirectory(variables_1.FilesPath);
directories = dirs.all
.filter(function (v) { return dirs.file.indexOf(v) == -1; })
.map(function (v) { return v.replace(variables_1.FilesPath, "./" + dir); });
.map(function (v) { return replacePath(v); });
filePromise = function (i) {
if (i === void 0) { i = 0; }
return new Promise(function (resolve) {
count.file++;
util_1.CopyFile(dir, files[i], ts, force)
util_1.CopyFile(files[i][0], files[i][1], ts, force)
.then(function (res) {
result.success = result.success && res.success;
if (res.skip)
Expand Down
25 changes: 12 additions & 13 deletions dist/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ var path_1 = __importDefault(require("path"));
var readline_1 = __importDefault(require("readline"));
var fs_1 = require("fs");
var child_process_1 = require("child_process");
var variables_1 = require("../variables");
var package_json_1 = require("./package-json");
var ExistCMD = {
npm: true
Expand Down Expand Up @@ -272,28 +271,28 @@ exports.WriteFile = function (dir, filename, content, force) {
});
}); }); });
};
exports.PathResolve = function (dir) {
exports.PathResolve = function (dir, root) {
if (dir === void 0) { dir = ''; }
return path_1["default"].resolve(__dirname, "../../" + dir);
if (root === void 0) { root = ''; }
return root ? path_1["default"].resolve(root, dir) : path_1["default"].resolve(__dirname, "../../" + dir);
};
exports.CopyFile = function (dir, filename, ts, force) {
exports.CopyFile = function (from, to, ts, force) {
if (ts === void 0) { ts = false; }
if (force === void 0) { force = false; }
return exports.Progress("Copy file " + exports.WarningMessage(filename), function () { return new Promise(function (resolve) { return __awaiter(void 0, void 0, void 0, function () {
var _filename, _path, _dir, __filename, result;
var filename = to.replace(/\.txt$/, '');
if (ts)
filename = filename.replace(/\.js(x)?$/, '.ts$1');
var dir = path_1["default"].dirname(filename), _filename = filename.replace(dir, '');
return exports.Progress("Copy file " + exports.WarningMessage(from) + " to " + exports.WarningMessage(filename), function () { return new Promise(function (resolve) { return __awaiter(void 0, void 0, void 0, function () {
var result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_filename = filename.replace('.txt', '');
if (ts)
_filename = _filename.replace(/\.js(x)?$/, '.ts$1');
_path = "" + dir + _filename, _dir = path_1["default"].dirname(_path), __filename = _path.replace(_dir, '');
return [4 /*yield*/, exports.CanWriteFile(_dir, __filename, force)];
case 0: return [4 /*yield*/, exports.CanWriteFile(dir, _filename, force)];
case 1:
result = _a.sent();
if (result.success) {
try {
fs_1.copyFileSync(exports.PathResolve("" + variables_1.FilesPath + filename), _path);
fs_1.copyFileSync(exports.PathResolve(from), filename);
}
catch (e) {
console.log(e);
Expand Down
27 changes: 11 additions & 16 deletions dist/lib/webpack.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion dist/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ program
.option('--minify', 'include code minifier plugin for Webpack')
.option('--watch', 'Webpack watch changes')
.option('--dev-server [port]', 'port of webpack-dev-server')
.option('--src <src>', 'entry directory for Webpack')
.option('--dist <dist>', 'output directory for Webpack')
.option('--typing', 'install typing packages')
.option('--no-install', 'do not install packages')
.action(function (appName, options) { return __awaiter(void 0, void 0, void 0, function () {
Expand All @@ -88,7 +90,9 @@ program
ts: options.ts,
minify: options.minify,
watch: options.watch,
devServer: options.devServer
devServer: options.devServer,
src: options.src,
dist: options.dist
}, options.force)];
case 3:
_a.sent();
Expand Down
12 changes: 11 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ interface IOptions extends OptionValues {
// webpack
watch?: boolean;
devServer?: boolean | number;
src?: string;
dist?: string;

clone?: boolean;
install?: boolean;
Expand Down Expand Up @@ -81,6 +83,8 @@ program
.option('--minify', 'include code minifier plugin for Webpack')
.option('--watch', 'Webpack watch changes')
.option('--dev-server [port]', 'port of webpack-dev-server')
.option('--src <src>', 'entry directory for Webpack')
.option('--dist <dist>', 'output directory for Webpack')
.option('--typing', 'install typing packages')
.option('--no-install', 'do not install packages')
.option('--no-clone', 'do not clone static files')
Expand Down Expand Up @@ -142,12 +146,18 @@ program
minify: options.minify,
watch: options.watch,
devServer: options.devServer,
src: options.src,
dist: options.dist,
}, options.force);

await CreatePackageJson(cwd, packageJSON, options.force);

if (options.clone) {
await CloneStaticFiles(cwd, options.ts, options.force);
await CloneStaticFiles(cwd, {
ts: options.ts,
src: options.src,
dist: options.dist,
}, options.force);
}

if (options.install) {
Expand Down

0 comments on commit cb24ffe

Please sign in to comment.