Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
444b054
commit ed36b96
Showing
5 changed files
with
572 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env node | ||
require('./dist/cli.js'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
Oops, something went wrong.