Skip to content

Commit

Permalink
add a CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Apr 13, 2018
1 parent 444b054 commit ed36b96
Show file tree
Hide file tree
Showing 5 changed files with 572 additions and 10 deletions.
2 changes: 2 additions & 0 deletions bin
@@ -0,0 +1,2 @@
#!/usr/bin/env node
require('./dist/cli.js');
14 changes: 10 additions & 4 deletions package.json
@@ -1,29 +1,35 @@
{
"name": "svelte-upgrade",
"name": "@sveltejs/upgrade",
"description": "Upgrade Svelte templates",
"version": "1.0.0",
"repository": "sveltejs/svelte-upgrade",
"main": "dist/svelte-upgrade.umd.js",
"module": "dist/svelte-upgrade.esm.js",
"main": "dist/index.js",
"bin": {
"svelte-upgrade": "bin"
},
"files": [
"dist"
],
"devDependencies": {
"esm": "^3.0.17",
"glob": "^7.1.2",
"rollup": "^0.52.0",
"rollup": "^0.57.1",
"rollup-plugin-json": "^2.3.0",
"sander": "^0.6.0",
"tape-modern": "^1.0.0"
},
"scripts": {
"build": "rollup -c",
"dev": "rollup -cw",
"test": "node -r esm test/test.js",
"prepublishOnly": "npm test && npm run build"
},
"license": "LIL",
"dependencies": {
"clorox": "^1.0.3",
"estree-walker": "^0.5.1",
"magic-string": "^0.24.0",
"prompts": "^0.1.8",
"sade": "^1.4.0",
"svelte": "^1.60.3"
}
Expand Down
18 changes: 12 additions & 6 deletions rollup.config.js
@@ -1,10 +1,16 @@
import json from 'rollup-plugin-json';
import pkg from './package.json';

export default {
input: 'src/index.js',
output: [
{ file: pkg.main, format: 'umd' },
{ file: pkg.module, format: 'es' }
],
name: 'svelteUpgrade'
input: ['src/index.js', 'src/cli.js'],
output: {
dir: 'dist',
format: 'cjs'
},
experimentalCodeSplitting: true,
experimentalDynamicImport: true,
external: Object.keys(pkg.dependencies),
plugins: [
json()
]
};
80 changes: 80 additions & 0 deletions src/cli.js
@@ -0,0 +1,80 @@
import fs from 'fs';
import path from 'path';
import sade from 'sade';
import glob from 'glob';
import prompts from 'prompts';
import * as clorox from 'clorox';
import * as pkg from '../package.json';

const prog = sade('svelte-upgrade').version(pkg.version);

function mkdirp(dir) {
const parent = path.dirname(dir);
if (parent !== dir) mkdirp(parent);

try {
fs.mkdirSync(dir);
} catch (err) {
// noop
}
}

prog.command(`v2 <input>`)
.describe(`upgrade <input> file/directory to v2 syntax`)
.option(`-o, --output`, `Write new files, instead of overwriting input files`)
.option(`-f, --force`, `Don't ask before overwriting files`)
.example(`v2 MyComponent.html`)
.example(`v2 MyComponent.html -o My-Component.v2.html`)
.example(`v2 src`)
.action(async (input, opts) => {
try {
const stats = fs.statSync(input);

const upgrade = await import('./index.js');

let output = input;

if (stats.isDirectory()) {
const files = glob.sync('**/*.+(html|svelte)', { cwd: input });
input = files.map(file => path.join(input, file));
output = files.map(file => path.join(output, file));
} else {
input = [input];
output = [output];
}

if (!opts.force) {
const existing = output.filter(file => fs.existsSync(file));
if (existing.length > 0) {
console.error(`${clorox.cyan(`This will overwrite the following files:`)}`);
console.error(`${clorox.gray(existing.join('\n'))}`)

const response = await prompts({
type: 'confirm',
name: 'value',
message: `Overwrite ${existing.length} ${existing.length === 1 ? 'file' : 'files'}?`,
initial: true
});

if (response.value === false) {
console.error(`${clorox.cyan('Aborted')}`);
return;
}
}
}

input.forEach((src, i) => {
const dest = output[i];
const upgraded = upgrade.upgradeTemplate(fs.readFileSync(src, 'utf-8'));

mkdirp(path.dirname(dest));
fs.writeFileSync(dest, upgraded);
});

console.error(`${clorox.cyan(`Wrote ${output.length} ${output.length === 1 ? 'file' : 'files'}`)}`)
} catch (err) {
console.error(`${clorox.red(err.message)}`);
}
});

prog.parse(process.argv);

0 comments on commit ed36b96

Please sign in to comment.