2020-02-14 17:02:57 +00:00
|
|
|
'use strict';
|
|
|
|
|
2022-01-14 12:44:48 +00:00
|
|
|
const fetch = require('node-fetch');
|
|
|
|
const fs = require('fs');
|
2020-02-14 17:02:57 +00:00
|
|
|
const fse = require('fs-extra');
|
2022-01-14 12:44:48 +00:00
|
|
|
const glob = require('glob').sync;
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
const KEEP_DIRECTORIES = null;
|
2020-02-14 17:02:57 +00:00
|
|
|
|
|
|
|
const files = {
|
|
|
|
'codemirror': [
|
2022-01-14 12:44:48 +00:00
|
|
|
KEEP_DIRECTORIES,
|
2020-02-14 17:02:57 +00:00
|
|
|
'addon/comment/comment.js',
|
|
|
|
'addon/dialog',
|
|
|
|
'addon/edit/closebrackets.js',
|
|
|
|
'addon/edit/matchbrackets.js',
|
|
|
|
'addon/fold/brace-fold.js',
|
|
|
|
'addon/fold/comment-fold.js',
|
|
|
|
'addon/fold/foldcode.js',
|
|
|
|
'addon/fold/foldgutter.*',
|
|
|
|
'addon/fold/indent-fold.js',
|
|
|
|
'addon/hint/css-hint.js',
|
|
|
|
'addon/hint/show-hint.*',
|
|
|
|
'addon/lint/css-lint.js',
|
|
|
|
'addon/lint/json-lint.js',
|
|
|
|
'addon/lint/lint.*',
|
|
|
|
'addon/scroll/annotatescrollbar.js',
|
|
|
|
'addon/search/matchesonscrollbar.*',
|
|
|
|
'addon/search/searchcursor.js',
|
|
|
|
'addon/selection/active-line.js',
|
|
|
|
'keymap/*',
|
|
|
|
'lib/*',
|
|
|
|
'mode/css',
|
|
|
|
'mode/javascript',
|
|
|
|
'mode/stylus',
|
2020-11-18 11:17:15 +00:00
|
|
|
'theme/*',
|
2020-02-14 17:02:57 +00:00
|
|
|
],
|
|
|
|
'jsonlint': [
|
2022-01-14 12:44:48 +00:00
|
|
|
'lib/jsonlint.js',
|
|
|
|
'README.md -> LICENSE',
|
2020-02-14 17:02:57 +00:00
|
|
|
],
|
|
|
|
'less-bundle': [
|
2022-01-14 12:44:48 +00:00
|
|
|
'dist/less.min.js',
|
2020-02-14 17:02:57 +00:00
|
|
|
],
|
|
|
|
'lz-string-unsafe': [
|
2020-11-18 11:17:15 +00:00
|
|
|
'lz-string-unsafe.min.js',
|
2020-02-14 17:02:57 +00:00
|
|
|
],
|
|
|
|
'stylelint-bundle': [
|
2022-01-14 12:44:48 +00:00
|
|
|
'dist/stylelint-bundle.min.js',
|
|
|
|
'https://github.com/stylelint/stylelint/raw/{VERSION}/LICENSE',
|
2020-02-14 17:02:57 +00:00
|
|
|
],
|
|
|
|
'stylus-lang-bundle': [
|
2022-01-14 12:44:48 +00:00
|
|
|
'dist/stylus-renderer.min.js',
|
2020-02-14 17:02:57 +00:00
|
|
|
],
|
|
|
|
'usercss-meta': [
|
2022-01-14 12:44:48 +00:00
|
|
|
'dist/usercss-meta.min.js',
|
2020-02-14 17:02:57 +00:00
|
|
|
],
|
|
|
|
'db-to-cloud': [
|
2022-01-14 12:44:48 +00:00
|
|
|
'dist/db-to-cloud.min.js',
|
2020-02-14 17:02:57 +00:00
|
|
|
],
|
2020-08-31 08:38:18 +00:00
|
|
|
'webext-launch-web-auth-flow': [
|
2022-01-14 12:44:48 +00:00
|
|
|
'dist/webext-launch-web-auth-flow.min.js',
|
|
|
|
],
|
|
|
|
'@eight04/draggable-list': [
|
|
|
|
'dist/draggable-list.iife.min.js',
|
2020-11-18 11:17:15 +00:00
|
|
|
],
|
2020-02-14 17:02:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
main().catch(console.error);
|
|
|
|
|
|
|
|
async function main() {
|
2021-01-01 15:32:04 +00:00
|
|
|
fse.emptyDirSync('vendor');
|
2022-01-14 12:44:48 +00:00
|
|
|
await Promise.all(Object.keys(files).map(async pkg => {
|
|
|
|
console.log(`Building ${pkg}...`);
|
|
|
|
const pkgName = getFileName(pkg);
|
|
|
|
const flatPkg = pkg === pkgName || files[pkgName]
|
|
|
|
? pkg.replace(/\//g, '-')
|
|
|
|
: pkgName;
|
|
|
|
const res = await buildFiles(pkg, flatPkg, files[pkg]);
|
|
|
|
buildLicense(pkg, flatPkg);
|
|
|
|
buildReadme(pkg, flatPkg, res);
|
|
|
|
}));
|
|
|
|
console.log('Building CodeMirror theme list...');
|
|
|
|
buildThemeList();
|
2020-02-14 17:02:57 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 12:44:48 +00:00
|
|
|
async function buildFiles(pkg, flatPkg, patterns) {
|
|
|
|
const keepDirs = patterns.includes(KEEP_DIRECTORIES);
|
|
|
|
let fetched = '';
|
|
|
|
let copied = '';
|
2020-02-14 17:02:57 +00:00
|
|
|
for (let pattern of patterns) {
|
2022-01-14 12:44:48 +00:00
|
|
|
if (pattern === KEEP_DIRECTORIES) continue;
|
2020-02-14 17:02:57 +00:00
|
|
|
pattern = pattern.replace('{VERSION}', require(`${pkg}/package.json`).version);
|
2022-01-14 12:44:48 +00:00
|
|
|
const [src, dest = !keepDirs && getFileName(src)] = pattern.split(/\s*->\s*/);
|
|
|
|
if (/^https?:/.test(src)) {
|
|
|
|
fse.outputFileSync(`vendor/${flatPkg}/${dest}`, await (await fetch(src)).text());
|
|
|
|
fetched += `* ${dest}: ${src}\n`;
|
2020-02-14 17:02:57 +00:00
|
|
|
} else {
|
2022-01-14 12:44:48 +00:00
|
|
|
const files = glob(`node_modules/${pkg}/${src}`);
|
|
|
|
if (!files.length) {
|
2020-02-20 12:17:15 +00:00
|
|
|
throw new Error(`Pattern ${src} matches no files`);
|
2020-02-14 17:02:57 +00:00
|
|
|
}
|
2022-01-14 12:44:48 +00:00
|
|
|
for (const file of files) {
|
|
|
|
fse.copySync(file, dest
|
|
|
|
? `vendor/${flatPkg}/${dest}`
|
|
|
|
: `vendor/${path.relative('node_modules', file).replace(pkg + '/', flatPkg + '/')}`);
|
2022-01-14 15:46:03 +00:00
|
|
|
copied += `* ${reportFile(pkg, file, dest)}\n`;
|
2022-01-14 12:44:48 +00:00
|
|
|
}
|
2020-02-14 17:02:57 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-14 12:44:48 +00:00
|
|
|
return {fetched, copied};
|
2020-02-14 17:02:57 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 12:44:48 +00:00
|
|
|
function buildLicense(pkg, flatPkg) {
|
|
|
|
const LICENSE = `vendor/${flatPkg}/LICENSE`;
|
|
|
|
if (!fs.existsSync(LICENSE)) {
|
|
|
|
const [src] = glob(`node_modules/${pkg}/LICEN[SC]E*`);
|
|
|
|
if (!src) throw new Error(`Cannot find license file for ${pkg}`);
|
|
|
|
fse.copySync(src, LICENSE);
|
2020-02-14 17:02:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-14 12:44:48 +00:00
|
|
|
function buildReadme(pkg, flatPkg, {fetched, copied}) {
|
|
|
|
const {name, version} = require(`${pkg}/package.json`);
|
|
|
|
fse.outputFileSync(`vendor/${flatPkg}/README.md`, [
|
|
|
|
`## ${name} v${version}`,
|
|
|
|
fetched && `Files downloaded from URL:\n${fetched}`,
|
|
|
|
copied && `Files copied from NPM (node_modules):\n${copied}`,
|
|
|
|
].filter(Boolean).join('\n\n'));
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildThemeList() {
|
|
|
|
fse.outputFileSync('edit/codemirror-themes.js', deindent(`\
|
|
|
|
/* Do not edit. This file is auto-generated by build-vendor.js */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/* exported CODEMIRROR_THEMES */
|
|
|
|
const CODEMIRROR_THEMES = [
|
|
|
|
${
|
|
|
|
fs.readdirSync('vendor/codemirror/theme')
|
|
|
|
.filter(name => name.endsWith('.css'))
|
|
|
|
.map(name => name.replace('.css', ''))
|
|
|
|
.sort()
|
|
|
|
.map(t => ` '${t.replace(/'/g, '\\$&')}',`).join('\n')
|
2020-02-14 17:02:57 +00:00
|
|
|
}
|
2022-01-14 12:44:48 +00:00
|
|
|
];
|
|
|
|
`));
|
|
|
|
}
|
|
|
|
|
|
|
|
function deindent(str) {
|
|
|
|
const indent = str.match(/^\s*/)[0];
|
|
|
|
return indent
|
|
|
|
? str.replace(new RegExp('^' + indent, 'gm'), '')
|
|
|
|
: str;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFileName(path) {
|
|
|
|
return path.split('/').pop();
|
2020-02-14 17:02:57 +00:00
|
|
|
}
|
2022-01-14 15:46:03 +00:00
|
|
|
|
|
|
|
function reportFile(pkg, file, dest) {
|
|
|
|
file = path.relative(`node_modules/${pkg}`, file).replace(/\\/g, '/');
|
|
|
|
if (!dest || dest === file) {
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
if (file.includes('/') && getFileName(dest) === getFileName(file)) {
|
|
|
|
file = file.replace(/[^/]+$/, '*');
|
|
|
|
}
|
|
|
|
return `${dest}: ${file}`;
|
|
|
|
}
|