mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-18 05:25:08 +00:00
implement sapper upgrade
This commit is contained in:
@@ -1,8 +1,9 @@
|
|||||||
import mri from 'mri';
|
import mri from 'mri';
|
||||||
import chalk from 'chalk';
|
import chalk from 'chalk';
|
||||||
import help from './help.md';
|
import help from './help.md';
|
||||||
import build from './build.js';
|
import build from './build';
|
||||||
import exporter from './export.js';
|
import exporter from './export';
|
||||||
|
import upgrade from './upgrade';
|
||||||
import { dest, entry, src } from '../config';
|
import { dest, entry, src } from '../config';
|
||||||
import * as pkg from '../../package.json';
|
import * as pkg from '../../package.json';
|
||||||
|
|
||||||
@@ -46,4 +47,8 @@ if (cmd === 'build') {
|
|||||||
.catch(err => {
|
.catch(err => {
|
||||||
console.error(err ? err.details || err.stack || err.message || err : 'Unknown error');
|
console.error(err ? err.details || err.stack || err.message || err : 'Unknown error');
|
||||||
});
|
});
|
||||||
|
} else if (cmd === 'upgrade') {
|
||||||
|
upgrade();
|
||||||
|
} else {
|
||||||
|
console.log(`unrecognized command ${cmd} — try \`sapper --help\` for more information`);
|
||||||
}
|
}
|
||||||
|
|||||||
53
src/cli/upgrade.ts
Normal file
53
src/cli/upgrade.ts
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
import * as fs from 'fs';
|
||||||
|
import chalk from 'chalk';
|
||||||
|
|
||||||
|
export default async function upgrade() {
|
||||||
|
const upgraded = [
|
||||||
|
|
||||||
|
].filter(Boolean);
|
||||||
|
await upgrade_sapper_main()
|
||||||
|
if (upgraded.length === 0) {
|
||||||
|
console.log(`No changes!`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function upgrade_sapper_main() {
|
||||||
|
const _2xx = read('templates/2xx.html');
|
||||||
|
const _4xx = read('templates/4xx.html');
|
||||||
|
const _5xx = read('templates/5xx.html');
|
||||||
|
|
||||||
|
const pattern = /<script src='\%sapper\.main\%'><\/script>/;
|
||||||
|
|
||||||
|
let replaced = false;
|
||||||
|
|
||||||
|
['2xx', '4xx', '5xx'].forEach(code => {
|
||||||
|
const file = `templates/${code}.html`
|
||||||
|
const template = read(file);
|
||||||
|
if (!template) return;
|
||||||
|
|
||||||
|
if (/\%sapper\.main\%/.test(template)) {
|
||||||
|
if (!pattern.test(template)) {
|
||||||
|
console.log(chalk.red(`Could not replace %sapper.main% in ${file}`));
|
||||||
|
} else {
|
||||||
|
write(file, template.replace(pattern, `%sapper.scripts%`));
|
||||||
|
console.log(chalk.green(`Replaced %sapper.main% in ${file}`));
|
||||||
|
replaced = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return replaced;
|
||||||
|
}
|
||||||
|
|
||||||
|
function read(file: string) {
|
||||||
|
try {
|
||||||
|
return fs.readFileSync(file, 'utf-8');
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function write(file: string, data: string) {
|
||||||
|
fs.writeFileSync(file, data);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user