Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules
npm-debug.log
.DS_Store
dist
dist.js
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <OUT_DIR> <FILE>
> restore-source-tree --out-dir <OUT_DIR> <FILE1> <FILE2> <FILE3>
```
2 changes: 1 addition & 1 deletion bin/restore-source-tree.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#! /usr/bin/env node

require('../dist/index.js');
require('../dist.js');
33 changes: 22 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] <file>')
.description('Restores file structure from source map')
.option('-o, --out-dir [dir]', 'Output directory (\'output\' by default)', 'output')
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
});

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]> (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"
Expand Down