You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.0 KiB
JavaScript
77 lines
2.0 KiB
JavaScript
"use strict";
|
|
|
|
const Promise = require("bluebird");
|
|
const fs = require("fs");
|
|
const fsPromises = require("fs").promises;
|
|
const path = require("path");
|
|
const escapeStringRegexp = require("escape-string-regexp");
|
|
|
|
const pipe = require("@promistream/pipe");
|
|
const walkFolder = require("@promistream/walk-folder");
|
|
const map = require("@promistream/map");
|
|
const filter = require("@promistream/filter");
|
|
const collect = require("@promistream/collect");
|
|
|
|
if (process.argv[2] == null) {
|
|
throw new Error(`Must specify a path`);
|
|
}
|
|
|
|
let boilerplate = fs.readFileSync(path.join(__dirname, "_boilerplate.js"), "utf8");
|
|
|
|
function parseBoilerplate(code) {
|
|
let lines = code.split("\n");
|
|
let items = [];
|
|
let currentItem = "";
|
|
|
|
for (let line of lines) {
|
|
if (line.trim() === "//##--") {
|
|
items.push(currentItem);
|
|
currentItem = "";
|
|
} else {
|
|
currentItem += line + "\n";
|
|
}
|
|
}
|
|
|
|
items.push(currentItem);
|
|
|
|
return items.filter((item) => item.trim() !== "");
|
|
}
|
|
|
|
let regexes = parseBoilerplate(boilerplate)
|
|
.map((text) => escapeStringRegexp(text).trim().replace(/\s+/g, "\\s+"))
|
|
.map((regex) => new RegExp(regex, "g"));
|
|
|
|
return Promise.try(() => {
|
|
return pipe([
|
|
walkFolder(process.argv[2], {
|
|
throughStream: filter((path) => !path.includes("node_modules"))
|
|
}),
|
|
filter((item) => !item.stats.isDirectory()),
|
|
filter((item) => item.path.match(/\.(?:c|m)?jsx?$/)),
|
|
map((item) => {
|
|
return Promise.try(() => {
|
|
return fsPromises.readFile(item.path, "utf8");
|
|
}).then((contents) => {
|
|
let replaceCount = 0;
|
|
|
|
for (let [ index, regex ] of Object.entries(regexes)) {
|
|
contents = contents.replace(regex, () => {
|
|
// console.log(" - " + regex);
|
|
replaceCount += 1;
|
|
return `// Transpiler boilerplate removed by boilerplate-remover (rule ${index})`;
|
|
});
|
|
}
|
|
|
|
if (replaceCount > 0) {
|
|
console.log(`Replaced ${replaceCount} items in ${item.path}`);
|
|
|
|
return fsPromises.writeFile(item.path, contents, "utf8");
|
|
}
|
|
});
|
|
}),
|
|
collect()
|
|
]).read();
|
|
}).then(() => {
|
|
console.log("Completed!");
|
|
});
|