Comparing version 1.1.1 to 1.2.0
@@ -12,14 +12,124 @@ #!/usr/bin/env node | ||
const unpack_js_1 = __importDefault(require("./unpack.js")); | ||
const command_line_args_1 = __importDefault(require("command-line-args")); | ||
const command_line_usage_1 = __importDefault(require("command-line-usage")); | ||
const currDir = process.cwd(); | ||
const outDir = path_1.default.join(currDir, "out"); | ||
if (fs_1.default.existsSync(outDir)) { | ||
rimraf_1.sync(outDir); | ||
const helpInfo = [ | ||
{ | ||
header: "DPacker", | ||
content: "A tool to debundle webpack modules", | ||
}, | ||
{ | ||
header: "Example Usage", | ||
content: "npx dpacker ./assets -b -v", | ||
}, | ||
{ | ||
header: "Options", | ||
optionList: [ | ||
{ | ||
name: "input", | ||
alias: "i", | ||
typeLabel: "{underline directory}", | ||
description: "The input folder to debundle", | ||
}, | ||
{ | ||
name: "outDir", | ||
alias: "o", | ||
typeLabel: "{underline ./out}", | ||
description: "The output folder to save the debundled files.", | ||
}, | ||
{ | ||
name: "manifest", | ||
alias: "m", | ||
typeLabel: "{underline manifest.json}", | ||
description: "The manifest file path to generate", | ||
}, | ||
{ | ||
name: "verbose", | ||
alias: "v", | ||
description: "Prints verbose output", | ||
type: Boolean, | ||
}, | ||
{ | ||
name: "beautify", | ||
alias: "b", | ||
description: "Beautifies the output", | ||
type: Boolean, | ||
}, | ||
{ | ||
name: "allowDuplicates", | ||
alias: "d", | ||
description: "Allows duplicate files to be generated in format 'fileName-n'", | ||
type: Boolean, | ||
}, | ||
{ | ||
name: "force", | ||
alias: "f", | ||
description: "Forces the tool to overwrite existing files", | ||
type: Boolean, | ||
}, | ||
{ | ||
name: "help", | ||
alias: "h", | ||
description: "Prints this help message", | ||
type: Boolean, | ||
}, | ||
], | ||
}, | ||
{ | ||
header: "Source & Support", | ||
content: "Available on Github at https://github.com/meguminsama/dpacker", | ||
}, | ||
]; | ||
const usageHelp = command_line_usage_1.default(helpInfo); | ||
const argDefinitions = [ | ||
{ name: "input", alias: "i", type: String, defaultOption: false }, | ||
{ | ||
name: "outDir", | ||
alias: "o", | ||
type: String, | ||
defaultValue: path_1.default.join(currDir, "out"), | ||
}, | ||
{ name: "manifest", alias: "m", type: String, defaultValue: null }, | ||
{ name: "verbose", alias: "v", type: Boolean, defaultValue: false }, | ||
{ name: "beautify", alias: "b", type: Boolean, defaultValue: false }, | ||
{ name: "allowDuplicates", alias: "d", type: Boolean, defaultValue: false }, | ||
{ name: "force", alias: "f", type: Boolean, defaultValue: false }, | ||
{ name: "help", alias: "h", type: Boolean, defaultValue: false }, | ||
]; | ||
const options = command_line_args_1.default(argDefinitions); | ||
if (options.help) { | ||
console.log(usageHelp); | ||
process.exit(); | ||
} | ||
fs_1.default.mkdirSync(outDir); | ||
if (process.argv.length < 3) | ||
console.log("invalid path provided..."), process.exit(); | ||
const dirName = process.argv[2]; | ||
const beautifyIt = process.argv[3] === "-b" || process.argv[4] === "-b"; | ||
const allowDuplicates = process.argv[3] === "-d" || process.argv[4] === "-d"; | ||
const files = fs_1.default.readdirSync(dirName); | ||
if (!options.input) { | ||
console.error("No input directory specified"); | ||
process.exit(1); | ||
} | ||
const dirName = options.input; | ||
let MANIFEST_PATH = ""; | ||
if (options.manifest) { | ||
if (typeof options.manifest === "string") { | ||
if (options.manifest.toLowerCase() !== "false") | ||
MANIFEST_PATH = options.manifest; | ||
} | ||
else if (typeof options.manifest === "boolean") { | ||
MANIFEST_PATH = path_1.default.join(options.outDir, "manifest.json"); | ||
} | ||
} | ||
if (!fs_1.default.existsSync(options.outDir)) { | ||
fs_1.default.mkdirSync(options.outDir); | ||
} | ||
else { | ||
if (options.force) { | ||
rimraf_1.sync(options.outDir); | ||
fs_1.default.mkdirSync(options.outDir); | ||
} | ||
else { | ||
console.error(`directory ${options.outDir} already exists...`); | ||
console.error(`use --force to overwrite the contents of this folder.`); | ||
process.exit(1); | ||
} | ||
} | ||
const files = fs_1.default.readdirSync(dirName).filter((f) => f.endsWith(".js")); | ||
const manifest = {}; | ||
for (const inFile of files) { | ||
@@ -32,2 +142,6 @@ const inPath = path_1.default.join(dirName, inFile); | ||
continue; | ||
const fileId = path_1.default.parse(inFile).base; | ||
if (options.manifest) { | ||
manifest[fileId] = { modules: {} }; | ||
} | ||
for (const item of data) { | ||
@@ -37,18 +151,26 @@ const newFileName = genNewFilePath(item.id); | ||
continue; | ||
const newFile = path_1.default.join(outDir, `${newFileName}.js`); | ||
console.log(newFile); | ||
if (beautifyIt) { | ||
const newFile = path_1.default.join(options.outDir, `${newFileName}.js`); | ||
if (options.beautify) { | ||
item.source = js_beautify_1.default(item.source); | ||
console.log(`${fileName} | Beautified: ${newFile}`); | ||
} | ||
if (options.manifest) { | ||
manifest[fileId].modules[item.id] = { | ||
fileName: path_1.default.parse(newFile).base, | ||
deps: Object.keys(item.deps), | ||
}; | ||
} | ||
fs_1.default.writeFileSync(newFile, item.source); | ||
console.log(`${fileName} | Written: ${newFile}`); | ||
if (options.verbose) | ||
console.log(`${fileName} | Written: ${newFile}`); | ||
} | ||
} | ||
if (options.manifest) { | ||
fs_1.default.writeFileSync(path_1.default.join(".", MANIFEST_PATH), JSON.stringify(manifest, null, 4)); | ||
} | ||
function genNewFilePath(fileName, i = 0) { | ||
if (!fs_1.default.existsSync(path_1.default.join(outDir, `${fileName}.js`))) | ||
if (!fs_1.default.existsSync(path_1.default.join(options.outDir, `${fileName}.js`))) | ||
return fileName; | ||
if (!allowDuplicates) | ||
if (!options.allowDuplicates) | ||
return undefined; | ||
const p = path_1.default.join(outDir, `${fileName}-${i}.js`); | ||
const p = path_1.default.join(options.outDir, `${fileName}-${i}.js`); | ||
if (fs_1.default.existsSync(p)) { | ||
@@ -55,0 +177,0 @@ genNewFilePath(fileName, ++i); |
{ | ||
"name": "dpacker", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"main": "dist/index.js", | ||
@@ -11,3 +11,3 @@ "license": "MIT", | ||
"scripts": { | ||
"start": "tsc; node ./dist/index.js", | ||
"start": "tsc && node ./dist/index.js", | ||
"build": "tsc" | ||
@@ -22,2 +22,5 @@ }, | ||
"astring": "^1.8.1", | ||
"chalk": "^5.0.1", | ||
"command-line-args": "^5.2.1", | ||
"command-line-usage": "^6.1.2", | ||
"js-beautify": "^1.14.0", | ||
@@ -29,2 +32,4 @@ "multisplice": "^1.0.0", | ||
"devDependencies": { | ||
"@types/command-line-args": "^5.2.0", | ||
"@types/command-line-usage": "^5.0.2", | ||
"@types/estree": "^0.0.51", | ||
@@ -37,2 +42,2 @@ "@types/js-beautify": "^1.13.3", | ||
} | ||
} | ||
} |
@@ -18,2 +18,15 @@ # DPacker | ||
## Parameters | ||
| Flag name | Shorthand | Default Value | Purpose | | ||
| ----------------- | --------- | ------------- | ---------------------------------------------------------------- | | ||
| --input | -i | | The input directory of .js files | | ||
| --outDir | -o | ./out | The file to output the separated files | | ||
| --manifest | -m | null | Generate a manifest file at the specified path | | ||
| --verbose | -v | false | Verbose output | | ||
| --beautify | -b | false | Beautify the outputted javascript files | | ||
| --allowDuplicates | -d | false | Allows duplicate files to be generated when detected | | ||
| --force | -f | false | If the output directory already exists, use this to overwrite it | | ||
| --help | -h | | Show the help menu | | ||
## Flags: | ||
@@ -20,0 +33,0 @@ |
191
src/index.ts
@@ -6,23 +6,155 @@ import fs from "fs"; | ||
import unpack from "./unpack.js"; | ||
import commandLineArgs from "command-line-args"; | ||
import commandLineUsage from "command-line-usage"; | ||
const currDir = process.cwd(); | ||
const outDir = path.join(currDir, "out"); | ||
// delete & remake out dir | ||
if (fs.existsSync(outDir)) { | ||
rimraf(outDir); | ||
const helpInfo: commandLineUsage.Section[] = [ | ||
{ | ||
header: "DPacker", | ||
content: "A tool to debundle webpack modules", | ||
}, | ||
{ | ||
header: "Example Usage", | ||
content: "npx dpacker ./assets -b -v", | ||
}, | ||
{ | ||
header: "Options", | ||
optionList: [ | ||
{ | ||
name: "input", | ||
alias: "i", | ||
typeLabel: "{underline directory}", | ||
description: "The input folder to debundle", | ||
}, | ||
{ | ||
name: "outDir", | ||
alias: "o", | ||
typeLabel: "{underline ./out}", | ||
description: "The output folder to save the debundled files.", | ||
}, | ||
{ | ||
name: "manifest", | ||
alias: "m", | ||
typeLabel: "{underline manifest.json}", | ||
description: "The manifest file path to generate", | ||
}, | ||
{ | ||
name: "verbose", | ||
alias: "v", | ||
description: "Prints verbose output", | ||
type: Boolean, | ||
}, | ||
{ | ||
name: "beautify", | ||
alias: "b", | ||
description: "Beautifies the output", | ||
type: Boolean, | ||
}, | ||
{ | ||
name: "allowDuplicates", | ||
alias: "d", | ||
description: | ||
"Allows duplicate files to be generated in format 'fileName-n'", | ||
type: Boolean, | ||
}, | ||
{ | ||
name: "force", | ||
alias: "f", | ||
description: "Forces the tool to overwrite existing files", | ||
type: Boolean, | ||
}, | ||
{ | ||
name: "help", | ||
alias: "h", | ||
description: "Prints this help message", | ||
type: Boolean, | ||
}, | ||
], | ||
}, | ||
{ | ||
header: "Source & Support", | ||
content: "Available on Github at https://github.com/meguminsama/dpacker", | ||
}, | ||
]; | ||
const usageHelp = commandLineUsage(helpInfo); | ||
const argDefinitions: commandLineArgs.OptionDefinition[] = [ | ||
{ name: "input", alias: "i", type: String, defaultOption: false }, | ||
{ | ||
name: "outDir", | ||
alias: "o", | ||
type: String, | ||
defaultValue: path.join(currDir, "out"), | ||
}, | ||
{ name: "manifest", alias: "m", type: String, defaultValue: null }, | ||
{ name: "verbose", alias: "v", type: Boolean, defaultValue: false }, | ||
{ name: "beautify", alias: "b", type: Boolean, defaultValue: false }, | ||
{ name: "allowDuplicates", alias: "d", type: Boolean, defaultValue: false }, | ||
{ name: "force", alias: "f", type: Boolean, defaultValue: false }, | ||
{ name: "help", alias: "h", type: Boolean, defaultValue: false }, | ||
]; | ||
const options: { | ||
verbose: boolean; | ||
beautify: boolean; | ||
allowDuplicates: boolean; | ||
outDir: string; | ||
input: string; | ||
manifest: string | boolean | null; | ||
force: boolean; | ||
help: boolean; | ||
} = commandLineArgs(argDefinitions) as any; | ||
if (options.help) { | ||
console.log(usageHelp); | ||
process.exit(); | ||
} | ||
fs.mkdirSync(outDir); | ||
// must be at least 3 args | ||
if (process.argv.length < 3) | ||
console.log("invalid path provided..."), process.exit(); | ||
if (!options.input) { | ||
console.error("No input directory specified"); | ||
process.exit(1); | ||
} | ||
const dirName = process.argv[2]; | ||
const dirName = options.input; | ||
const beautifyIt = process.argv[3] === "-b" || process.argv[4] === "-b"; | ||
const allowDuplicates = process.argv[3] === "-d" || process.argv[4] === "-d"; | ||
let MANIFEST_PATH = ""; | ||
const files = fs.readdirSync(dirName); | ||
if (options.manifest) { | ||
if (typeof options.manifest === "string") { | ||
if (options.manifest.toLowerCase() !== "false") | ||
MANIFEST_PATH = options.manifest; | ||
} else if (typeof options.manifest === "boolean") { | ||
MANIFEST_PATH = path.join(options.outDir, "manifest.json"); | ||
} | ||
} | ||
// delete & remake out dir | ||
if (!fs.existsSync(options.outDir)) { | ||
fs.mkdirSync(options.outDir); | ||
} else { | ||
if (options.force) { | ||
rimraf(options.outDir); | ||
fs.mkdirSync(options.outDir); | ||
} else { | ||
console.error(`directory ${options.outDir} already exists...`); | ||
console.error(`use --force to overwrite the contents of this folder.`); | ||
process.exit(1); | ||
} | ||
} | ||
const files = fs.readdirSync(dirName).filter((f) => f.endsWith(".js")); | ||
const manifest: { | ||
[k: string]: { | ||
modules: { | ||
[k: string]: { | ||
fileName: string; | ||
deps: string[]; | ||
}; | ||
}; | ||
}; | ||
} = {}; | ||
for (const inFile of files) { | ||
@@ -38,24 +170,43 @@ const inPath = path.join(dirName, inFile); | ||
const fileId = path.parse(inFile).base; | ||
if (options.manifest) { | ||
manifest[fileId] = { modules: {} }; | ||
} | ||
for (const item of data) { | ||
const newFileName = genNewFilePath(item.id); | ||
if (newFileName === undefined) continue; | ||
const newFile = path.join(outDir, `${newFileName}.js`); | ||
console.log(newFile); | ||
const newFile = path.join(options.outDir, `${newFileName}.js`); | ||
if (beautifyIt) { | ||
if (options.beautify) { | ||
item.source = jsBeautify(item.source); | ||
console.log(`${fileName} | Beautified: ${newFile}`); | ||
} | ||
if (options.manifest) { | ||
manifest[fileId].modules[item.id] = { | ||
fileName: path.parse(newFile).base, | ||
deps: Object.keys(item.deps), | ||
}; | ||
} | ||
fs.writeFileSync(newFile, item.source); | ||
console.log(`${fileName} | Written: ${newFile}`); | ||
if (options.verbose) console.log(`${fileName} | Written: ${newFile}`); | ||
} | ||
} | ||
if (options.manifest) { | ||
fs.writeFileSync( | ||
path.join(".", MANIFEST_PATH), | ||
JSON.stringify(manifest, null, 4) | ||
); | ||
} | ||
function genNewFilePath(fileName: string | number, i = 0) { | ||
if (!fs.existsSync(path.join(outDir, `${fileName}.js`))) return fileName; | ||
if (!fs.existsSync(path.join(options.outDir, `${fileName}.js`))) | ||
return fileName; | ||
if (!allowDuplicates) return undefined; | ||
if (!options.allowDuplicates) return undefined; | ||
const p = path.join(outDir, `${fileName}-${i}.js`); | ||
const p = path.join(options.outDir, `${fileName}-${i}.js`); | ||
@@ -62,0 +213,0 @@ if (fs.existsSync(p)) { |
29682
806
38
10
8
+ Addedchalk@^5.0.1
+ Addedcommand-line-args@^5.2.1
+ Addedcommand-line-usage@^6.1.2
+ Addedansi-styles@3.2.1(transitive)
+ Addedarray-back@3.1.04.0.2(transitive)
+ Addedchalk@2.4.25.4.1(transitive)
+ Addedcolor-convert@1.9.3(transitive)
+ Addedcolor-name@1.1.3(transitive)
+ Addedcommand-line-args@5.2.1(transitive)
+ Addedcommand-line-usage@6.1.3(transitive)
+ Addeddeep-extend@0.6.0(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedfind-replace@3.0.0(transitive)
+ Addedhas-flag@3.0.0(transitive)
+ Addedlodash.camelcase@4.3.0(transitive)
+ Addedreduce-flatten@2.0.0(transitive)
+ Addedsupports-color@5.5.0(transitive)
+ Addedtable-layout@1.0.2(transitive)
+ Addedtypical@4.0.05.2.0(transitive)
+ Addedwordwrapjs@4.0.1(transitive)