Skip to content

Instantly share code, notes, and snippets.

@vitalets
Created February 20, 2017 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vitalets/b3faa426f58809046818ceb78057bc59 to your computer and use it in GitHub Desktop.
Save vitalets/b3faa426f58809046818ceb78057bc59 to your computer and use it in GitHub Desktop.
msf unbundle
/**
* Unbundle samsung msf library:
* node msf-unbundle
*/
const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
const { SourceMapConsumer } = require('source-map');
const filename = './dist/msf-2.3.2.min.js.map';
const outdir = 'out';
const WEBPACK_PREFIX = 'msf:///';
const WEBPACK_FOOTER = '/*****************';
const readJson = filename => {
try {
return JSON.parse(fs.readFileSync(filename, 'utf8'));
} catch(e) {
console.error(`Parsing file '${filename}' failed: ${e.message}`);
process.exit(1);
}
}
const getSourceList = smc => {
let sources = smc.sources
.filter(src => src.startsWith(WEBPACK_PREFIX))
.map(src => [cleanFilepath(src), src])
.filter(([filePath]) => !filePath.startsWith('(webpack)'));
return sources;
}
const cleanFilepath = filepath => filepath.replace(WEBPACK_PREFIX, '').replace('?', '');
const trimFooter = str => str.substr(0, str.indexOf(WEBPACK_FOOTER)).trimRight() + '\n';
const saveSourceContent = (smc, filePath, src) => {
const content = trimFooter(smc.sourceContentFor(src));
const outPath = path.resolve(path.join(outdir, filePath));
const dir = path.dirname(outPath);
if (content.length < 2) return;
mkdirp(dir, err => {
if (err) {
console.error('Failed creating directory', dir);
process.exit(1);
} else {
fs.writeFile(outPath, content, err => {
if (err) {
console.error('Failed writing file', outPath);
process.exit(1);
} else {
console.log('saved', outPath)
}
});
}
})
}
function processFile(filename) {
const json = readJson(filename);
const smc = new SourceMapConsumer(json);
const sources = getSourceList(smc);
sources.forEach(([filePath, src]) => saveSourceContent(smc, filePath, src));
console.log(`Processed ${sources.length} files`);
}
fs.access(filename, err => {
if (err) {
console.error(err.message);
process.exit(1);
}
processFile(filename);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment