Skip to content

Instantly share code, notes, and snippets.

@stefanbuck
Created February 1, 2018 21:09
Show Gist options
  • Save stefanbuck/9ead57ec64d1c00f91759183deed48ae to your computer and use it in GitHub Desktop.
Save stefanbuck/9ead57ec64d1c00f91759183deed48ae to your computer and use it in GitHub Desktop.
const fs = require("fs-extra");
const path = require("path");
const rootDir = "~/projects/os/octolinker";
const pkg = require(`${rootDir}/packages/core/package.json`);
function packageJSON(name, dependencies) {
return {
name: `@octolinker/${name}`,
version: "1.0.0",
description: "",
repository: `https://github.com/octolinker/octolinker/tree/master/packages/${name}`,
license: "MIT",
main: "./index.js",
dependencies
};
}
const coreDepToRemove = {};
function call(sourcePath, prefix) {
sourcePath = `${rootDir}/${sourcePath}`;
const targetPath = `${rootDir}/packages`;
const name = path.basename(sourcePath).replace(".js", "");
const moduleName = `${prefix}-${name}`;
const modulePath = `${targetPath}/${moduleName}`;
fs.ensureDirSync(modulePath);
fs.moveSync(sourcePath, `${modulePath}/index.js`);
const dependencies = {};
// rewrite imports
let content = fs.readFileSync(`${modulePath}/index.js`).toString();
// rewrite helper imports
let resolverMatch2 = content.match(/.\/helper\/([^'"\s]+)/g) || [];
resolverMatch2.forEach(item => {
const resolverModule = item.replace(".js", "").replace("./helper/", "");
dependencies[`@octolinker/helper-${resolverModule}`] = "1.0.0";
console.log("item", item);
content = content
.toString()
.replace(item, `@octolinker/helper-${resolverModule}`);
});
// rewrite resolver imports
let resolverMatch = content.match(/..\/resolver\/([^'"\s]+)/g) || [];
resolverMatch.forEach(item => {
const resolverModule = item.replace(".js", "").replace("../resolver/", "");
dependencies[`@octolinker/resolver-${resolverModule}`] = "1.0.0";
content = content
.toString()
.replace(item, `@octolinker/resolver-${resolverModule}`);
});
// content = content.toString().replace(/\ from '\.\./g, ' from \'../core');
fs.writeFileSync(`${modulePath}/index.js`, content);
// inject dependencies to package.json
if (content.includes("@octolinker/helper-insert-link")) {
dependencies["@octolinker/helper-insert-link"] = "1.0.0";
}
if (content.includes("@octolinker/helper-grammar-regex-collection")) {
dependencies["@octolinker/helper-grammar-regex-collection"] = "1.0.0";
}
// find addition dependencies
let depMatch = content.match(/from '([a-z-A-Z'"\s]+)/g) || [];
depMatch.forEach(item => {
item = item.replace("from '", "").replace("'", "");
if (item !== "path") {
dependencies[item] = pkg.dependencies[item] || "tbd";
}
});
Object.assign(coreDepToRemove, dependencies);
// wirte package.json files
fs.writeJsonSync(
`${modulePath}/package.json`,
packageJSON(moduleName, dependencies),
{ spaces: 2 }
);
}
call("packages/core/plugins/helper/process-json.js", "helper");
call("packages/core/plugins/helper/regex-builder.js", "helper");
call("packages/core/plugins/helper/version.js", "helper");
call("packages/core/plugins/bower-manifest.js", "plugin");
call("packages/core/plugins/composer-manifest.js", "plugin");
call("packages/core/plugins/css.js", "plugin");
call("packages/core/plugins/docker.js", "plugin");
call("packages/core/plugins/dot-net-core.js", "plugin");
call("packages/core/plugins/dot-net.js", "plugin");
call("packages/core/plugins/gemfile-manifest.js", "plugin");
call("packages/core/plugins/go.js", "plugin");
call("packages/core/plugins/haskell.js", "plugin");
call("packages/core/plugins/homebrew-manifest.js", "plugin");
call("packages/core/plugins/html.js", "plugin");
call("packages/core/plugins/java.js", "plugin");
call("packages/core/plugins/javascript.js", "plugin");
call("packages/core/plugins/less.js", "plugin");
call("packages/core/plugins/nodejs-relative-path.js", "plugin");
call("packages/core/plugins/npm-manifest.js", "plugin");
call("packages/core/plugins/python.js", "plugin");
call("packages/core/plugins/requirements-txt.js", "plugin");
call("packages/core/plugins/ruby.js", "plugin");
call("packages/core/plugins/rust.js", "plugin");
call("packages/core/plugins/sass.js", "plugin");
call("packages/core/plugins/typescript.js", "plugin");
call("packages/core/plugins/vim.js", "plugin");
call("packages/core/resolver/git-url.js", "resolver");
call("packages/core/resolver/github-search.js", "resolver");
call("packages/core/resolver/github-shorthand.js", "resolver");
call("packages/core/resolver/live-resolver-query.js", "resolver");
call("packages/core/resolver/nuget.js", "resolver");
call("packages/core/resolver/relative-file.js", "resolver");
// Remove unused dependencies from core/package.json
Object.keys(coreDepToRemove).forEach(dep => {
delete pkg.dependencies[dep];
});
fs.writeJsonSync(`${rootDir}/packages/core/package.json`, pkg, { spaces: 2 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment