Skip to content

Commit

Permalink
feat(scaffolding): add node package scaffolding plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei199991 committed Jul 16, 2020
1 parent 1ec7803 commit e805658
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/cli-plugin-scaffold-node-package/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Change Log

All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
105 changes: 105 additions & 0 deletions packages/cli-plugin-scaffold-node-package/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
const fs = require("fs");
const path = require("path");
const util = require("util");
const ncp = util.promisify(require("ncp").ncp);
const findUp = require("find-up");
const kebabCase = require("lodash.kebabcase");

module.exports = [
{
name: "cli-plugin-scaffold-template-node-package",
type: "cli-plugin-scaffold-template",
scaffold: {
name: "Node Package",
questions: () => {
return [
{
name: "location",
message: "Enter package location (including package name)",
validate: location => {
if (location === "") {
return "Please enter a package location";
}

if (fs.existsSync(path.resolve(location))) {
return "The target location already exists";
}

return true;
}
}
];
},
generate: async ({ input }) => {
const { location } = input;
const fullLocation = path.resolve(location);

const packageName = kebabCase(location);

// Then we also copy the template folder
const sourceFolder = path.join(__dirname, "template");

if (fs.existsSync(location)) {
throw new Error(`Destination folder ${location} already exists!`);
}

await fs.mkdirSync(location, { recursive: true });

// Get base TS config & tsconfig.build path
const baseTsConfigPath = path
.relative(
fullLocation,
findUp.sync("tsconfig.json", {
cwd: fullLocation
})
)
.replace(/\\/g, "/");
const baseTsConfigBuildPath = path
.relative(
fullLocation,
findUp.sync("tsconfig.build.json", {
cwd: fullLocation
})
)
.replace(/\\/g, "/");

// Copy template files
await ncp(sourceFolder, location);

// Update the package's name
const packageJsonPath = path.resolve(location, "package.json");
let packageJson = fs.readFileSync(packageJsonPath, "utf8");
packageJson = packageJson.replace("[PACKAGE_NAME]", packageName);
fs.writeFileSync(packageJsonPath, packageJson);

// Get .babel.node.js path
const babelNodeJsPath = path
.relative(
fullLocation,
findUp.sync(".babel.node.js", {
cwd: fullLocation
})
)
.replace(/\\/g, "/");

// Update .babelrc.js
const babelrcPath = path.resolve(location, ".babelrc.js");
let babelrc = fs.readFileSync(babelrcPath, "utf8");
babelrc = babelrc.replace("[.BABEL.NODE_PATH]", babelNodeJsPath);
fs.writeFileSync(babelrcPath, babelrc);

// Update tsconfig "extends" path
const tsConfigPath = path.join(fullLocation, "tsconfig.json");
const tsconfig = require(tsConfigPath);
tsconfig.extends = baseTsConfigPath;
fs.writeFileSync(tsConfigPath, JSON.stringify(tsconfig, null, 2));

// Update tsconfig.build "extends" path
const tsconfigBuildPath = path.join(fullLocation, "tsconfig.build.json");
const tsconfigBuild = require(tsconfigBuildPath);
tsconfigBuild.extends = baseTsConfigBuildPath;
fs.writeFileSync(tsconfigBuildPath, JSON.stringify(tsconfigBuild, null, 2));
}
}
}
];
36 changes: 36 additions & 0 deletions packages/cli-plugin-scaffold-node-package/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@webiny/cli-plugin-scaffold-node-package",
"version": "4.4.0",
"description": "Generate a NodeJs package similar to our 'api-' packages",
"main": "index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/webiny/webiny-js.git",
"directory": "packages/cli-scaffold-graphql-service"
},
"author": "",
"license": "MIT",
"bugs": {
"url": "https://github.com/webiny/webiny-js/issues"
},
"homepage": "https://github.com/webiny/webiny-js#readme",
"dependencies": {
"find-up": "^4.1.0",
"lodash.kebabcase": "^4.1.1",
"ncp": "^2.0.0"
},
"devDependencies": {
"@shelf/jest-mongodb": "^1.1.5"
},
"publishConfig": {
"access": "public",
"directory": "."
},
"adio": {
"ignoreDirs": [
"template",
"node_modules",
"__tests__"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("[.BABEL.NODE_PATH]");
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const base = require("../../jest.config.base");

module.exports = base({ path: __dirname });
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "[PACKAGE_NAME]",
"version": "1.0.0",
"description": "",
"license": "MIT",
"scripts": {
"build": "rimraf ./dist *.tsbuildinfo && babel src -d dist --source-maps --copy-files --extensions \".ts,.tsx\""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import plugins from "./plugins.tsx";

export default () => [plugins];
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from "react";

export default {
name: "utility-plugins-remove-vowels",
type: "utility-plugins",
removeVowels: str => {
return str.replace(/[aeiouAEIOU]/g, "");
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../../tsconfig.build.json",
"include": ["src"],
"exclude": [
"node_modules",
"./dist"
],
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
"declarationDir": "./dist"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../tsconfig.json"
}

0 comments on commit e805658

Please sign in to comment.