-
Notifications
You must be signed in to change notification settings - Fork 613
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scaffolding): add node package scaffolding plugin
- Loading branch information
1 parent
1ec7803
commit e805658
Showing
10 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require("[.BABEL.NODE_PATH]"); |
3 changes: 3 additions & 0 deletions
3
packages/cli-plugin-scaffold-node-package/template/jest.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
9 changes: 9 additions & 0 deletions
9
packages/cli-plugin-scaffold-node-package/template/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\"" | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/cli-plugin-scaffold-node-package/template/src/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import plugins from "./plugins.tsx"; | ||
|
||
export default () => [plugins]; |
9 changes: 9 additions & 0 deletions
9
packages/cli-plugin-scaffold-node-package/template/src/plugins/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ""); | ||
} | ||
}; |
13 changes: 13 additions & 0 deletions
13
packages/cli-plugin-scaffold-node-package/template/tsconfig.build.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/cli-plugin-scaffold-node-package/template/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "../../tsconfig.json" | ||
} |