diff --git a/.gitignore b/.gitignore index 44b5048..c2fe761 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ node_modules npm-debug.log .DS_Store -dist +dist.js diff --git a/README.md b/README.md index 654c9e3..be8223e 100644 --- a/README.md +++ b/README.md @@ -6,5 +6,5 @@ Restores file structure from source map (only Webpack source map files supported ```sh > npm i -g restore-source-tree -> restore-source-tree --out-dir +> restore-source-tree --out-dir ``` diff --git a/bin/restore-source-tree.js b/bin/restore-source-tree.js index 144be16..cfe8f32 100644 --- a/bin/restore-source-tree.js +++ b/bin/restore-source-tree.js @@ -1,3 +1,3 @@ #! /usr/bin/env node -require('../dist/index.js'); +require('../dist.js'); diff --git a/index.js b/index.js index a9a9a3c..cca27f9 100644 --- a/index.js +++ b/index.js @@ -3,12 +3,14 @@ import path from 'path'; import mkdirp from 'mkdirp'; import { SourceMapConsumer } from 'source-map'; import { Command } from 'commander'; +import glob from 'glob'; +import { version } from './package.json'; const WEBPACK_PREFIX = 'webpack:///'; -const WEBPACK_FOOTER = '/** WEBPACK FOOTER **'; +const WEBPACK_FOOTER = [/\/*[*\s]+WEBPACK FOOTER/, /\/\/ WEBPACK FOOTER/]; const program = new Command('restore-source-tree') - .version('0.1.1') + .version(version) .usage('[options] ') .description('Restores file structure from source map') .option('-o, --out-dir [dir]', 'Output directory (\'output\' by default)', 'output') @@ -42,7 +44,16 @@ const getSourceList = smc => { return sources; } -const trimFooter = str => str.substr(0, str.indexOf(WEBPACK_FOOTER)).trimRight() + '\n'; +const trimFooter = (str) => { + const index = WEBPACK_FOOTER.reduce((result, footer) => { + if (result >= 0) return result; + const match = footer.exec(str); + if (!match) return -1; + return match.index; + }, -1); + if (index < 0) return str; + return str.substr(0, index).trimRight() + '\n'; +}; const saveSourceContent = (smc, filePath, src) => { const content = trimFooter(smc.sourceContentFor(src)); @@ -78,14 +89,14 @@ function processFile(filename) { console.log(`Processed ${sources.length} files`); } -const filename = program.args[0]; - -fs.access(filename, err => { - if (err) { +program.args +.map(pattern => glob.sync(pattern)) +.reduce((prev, curr) => prev.concat(curr), []) +.forEach((filename) => { + try { + fs.accessSync(filename); + processFile(filename); + } catch (err) { console.error(err.message); - process.exit(1); } - - processFile(filename); }); - diff --git a/package.json b/package.json index 37e3ff6..bf17853 100644 --- a/package.json +++ b/package.json @@ -2,19 +2,20 @@ "name": "restore-source-tree", "version": "0.1.1", "description": "Restores file structure from source map", - "main": "index.js", + "main": "dist.js", "bin": { "restore-source-tree": "bin/restore-source-tree.js" }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "build": "NODE_ENV=production babel index.js --out-dir dist", + "build": "NODE_ENV=production babel index.js --out-file dist.js", "prepublish": "npm run build" }, "author": "Alexander (http://kuzya.org/)", "license": "MIT", "dependencies": { "commander": "^2.9.0", + "glob": "^7.1.3", "minimist": "^1.2.0", "mkdirp": "^0.5.1", "source-map": "^0.5.6"