#!/usr/bin/env node /** * @license * Copyright (c) 2015 The Polymer Project Authors. All rights reserved. * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt * Code distributed by Google as part of the polymer project is also * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt */ 'use strict'; require("babel/polyfill"); import upgradeHtml from './upgrade_html'; import nomnomLib from 'nomnom'; import colors from 'colors/safe'; import fs from 'fs'; import path from 'path'; import walk from 'walk'; const nomnom = nomnomLib(); nomnom.script('polyup'); nomnom.option('overwrite', { flag: true, help: 'Overwrite the input html and any referenced scripts on disk.' }); nomnom.option('ignore', { abbr: 'i', list: true, metavar: 'PATH', default: [], help: 'File(s) that polyup should not upgrade, or even try to read at all. ' + ' Can be specified multiple times.' }); nomnom.option('webserver_root', { metavar: 'PATH', help: 'Base directory for resolving absolute URL paths.' }); nomnom.option('http_path_to_components', { metavar: 'PATH', help: 'Absolute site-local URL base for components. Used when polyup needs ' + 'to insert HTML imports. If not given, polyup will attempt to infer ' + 'it from other imports.' }); nomnom.option('recursive', { flag: true, help: 'Upgrade all files below the given directory. Starts with any HTML ' + ' files found and upgrades any javascript that is referenced from them.' }); nomnom.option('only_reformat', { flag: true, help: 'polyup necessarily makes some reformatting changes along with the ' + 'upgrades it performs. You can get a cleaner diff by first running ' + 'with this flag first, commit that, then run without it. When this ' + 'flag is given polyup will only make purely formatting-related changes.' }); nomnom.option('path_to_html', { position: 0, required: true, help: 'Path to the HTML file to upgrade. The first positional argument.' }); const opts = nomnom.parse(); const baseFilename = opts.path_to_html; if (!fs.existsSync(baseFilename)) { console.error('No such file: ' + baseFilename); process.exit(66); } const filesToUpgrade = []; if (!opts.recursive || !fs.statSync(baseFilename).isDirectory()) { filesToUpgrade.push(baseFilename); } else { walk.walkSync(baseFilename, {listeners:{ file(dirName, stat) { const fullPath = path.join(dirName, stat.name); if (fullPath.endsWith('.html')) { filesToUpgrade.push(fullPath); } } }}); } const toIgnore = new Set(); opts.ignore.forEach(function(pathToIgnore) { pathToIgnore = path.resolve(pathToIgnore); if (!fs.existsSync(pathToIgnore)) { console.error(`Ignored file doesn't exist at \`${pathToIgnore}\``); process.exit(66); } toIgnore.add(pathToIgnore); }); const componentsPath = opts.http_path_to_components; let webserverRoot; if (opts.webserver_root) { webserverRoot = path.resolve(opts.webserver_root); let stat; try { stat = fs.statSync(webserverRoot); } catch (_) { /* ignore */} if (!stat || !stat.isDirectory()) { console.error(`No directory at webserver_root path \`${webserverRoot}\``); process.exit(66); } } for (const filename of filesToUpgrade) { let outputMap; try { outputMap = upgradeHtml(filename, { toIgnore, webserverRoot, componentsPath, onlyReformat: opts.only_reformat }); } catch(e) { console.error('Error attempting to upgrade ' + filename + '\n\n'); throw e; } if (!opts.overwrite) { console.log(); for (let outFile in outputMap) { const contents = outputMap[outFile]; console.log(colors.blue(outFile)); for (let line of contents.split('\n')) { console.log(` ${line}`); } } } else { const tempPaths = {}; const cwd = process.cwd(); for (let outFile in outputMap) { if (!outFile.startsWith(cwd)) { if (webserverRoot) { if (!outFile.startsWith(webserverRoot)) { console.error( 'For safety polyup will only modify files under your ' + 'current working directory or the webserver root.'); console.error(`This file is outside both: ${outFile}'`); process.exit(78); } continue; } console.error( 'For safety polyup will only modify files under your ' + 'current working directory.'); console.error(`This file is outside your cwd: ${outFile}'`); process.exit(78); } } for (let outFile in outputMap) { // Put temp dirs in the same directory, to be confident that they'll be // in the same filesystem, and thus more likely for the moves to be atomic. let targetDir = path.dirname(outFile); let tempFilename; do { tempFilename = path.join(targetDir, '.polyup_temp_' + Math.random()); } while(fs.existsSync(tempFilename)); tempPaths[outFile] = tempFilename; fs.writeFileSync(tempFilename, outputMap[outFile]); } // Now that we've written all of the data out to disk we can just do move // operations, which should be atomic on most systems. for (let outFile in outputMap) { fs.renameSync(tempPaths[outFile], outFile); } } }