Skip to content

Commit

Permalink
feat: add create-father for create boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
PeachScript committed Aug 26, 2022
1 parent 41f631e commit 6010dbf
Show file tree
Hide file tree
Showing 19 changed files with 342 additions and 102 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/node_modules
/boilerplate/dist
/boilerplate/node_modules
/examples/*/compiled
/examples/*/dist
/examples/*/node_modules
Expand Down
3 changes: 3 additions & 0 deletions boilerplate/bin/create-father.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require('../dist/cli');
33 changes: 33 additions & 0 deletions boilerplate/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "create-father",
"version": "4.0.0-alpha.1",
"description": "Creator for father boilerplate",
"homepage": "https://github.com/umijs/father-next/tree/master/boilerplate#readme",
"bugs": "https://github.com/umijs/father-next/issues",
"repository": {
"type": "git",
"url": "https://github.com/umijs/father-next"
},
"license": "MIT",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"bin": {
"create-father": "bin/create-father.js"
},
"files": [
"dist"
],
"scripts": {
"build": "pnpm tsc",
"dev": "pnpm tsc --watch"
},
"dependencies": {
"@umijs/utils": "^4.0.0"
},
"publishConfig": {
"access": "public"
},
"authors": [
"PeachScript <[email protected]> (https://github.com/PeachScript)"
]
}
26 changes: 26 additions & 0 deletions boilerplate/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { chalk, isLocalDev, yParser } from '@umijs/utils';

const args = yParser(process.argv.slice(2), {
alias: {
version: ['v'],
help: ['h'],
},
boolean: ['version'],
});

if (args.version && !args._[0]) {
args._[0] = 'version';
const local = isLocalDev() ? chalk.cyan('@local') : '';
const { name, version } = require('../package.json');
console.log(`${name}@${version}${local}`);
} else {
require('./')
.default({
cwd: process.cwd(),
args,
})
.catch((err: Error) => {
console.error(`Create failed, ${err.message}`);
console.error(err);
});
}
87 changes: 87 additions & 0 deletions boilerplate/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import {
BaseGenerator,
installWithNpmClient,
prompts,
yParser,
} from '@umijs/utils';
import { join } from 'path';

export default async ({
cwd,
args,
}: {
cwd: string;
args: yParser.Arguments;
}) => {
const [name] = args._;
const target = name ? join(cwd, name) : cwd;
const registry = 'https://registry.npmjs.org/';
const { version } = require('../package.json');
const { npmClient, platform } = await prompts(
[
{
type: 'select',
name: 'platform',
message: 'Pick target platform(s)',
choices: [
{ title: 'Node.js', value: 'node' },
{ title: 'Browser', value: 'browser' },
{ title: 'Both', value: 'both' },
],
initial: 0,
},
{
type: 'select',
name: 'npmClient',
message: 'Pick NPM client',
choices: [
{ title: 'npm', value: 'npm' },
{ title: 'cnpm', value: 'cnpm' },
{ title: 'tnpm', value: 'tnpm' },
{ title: 'yarn', value: 'yarn' },
{ title: 'pnpm', value: 'pnpm' },
],
initial: 4,
},
],
{
onCancel() {
process.exit(1);
},
},
);

const generator = new BaseGenerator({
path: join(__dirname, '../template'),
target,
data: {
version: version.includes('-canary.') ? version : `^${version}`,
npmClient,
isNode: platform === 'node',
isBrowser: platform === 'browser',
isBothNodeBrowser: platform === 'both',
registry,
},
questions: [
{
name: 'name',
type: 'text',
message: `Input NPM package name`,
},
{
name: 'description',
type: 'text',
message: `Input NPM package description`,
},
{
name: 'author',
type: 'text',
message: `Input NPM package author (Name <[email protected]>)`,
},
],
});
await generator.run();

// install
installWithNpmClient({ npmClient, cwd: target });
};
7 changes: 7 additions & 0 deletions boilerplate/template/.fatherrc.ts.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'father';

export default defineConfig({
{{#isBoth}}esm: { input: 'src/client' },
cjs: { input: 'src/server' },{{/isBoth}}{{#isNode}}cjs: {},{{/isNode}}{{#isBrowser}}esm: {},{{/isBrowser}}
prebundle: {},
});
3 changes: 3 additions & 0 deletions boilerplate/template/.gitignore.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/node_modules
/dist
.DS_Store
23 changes: 23 additions & 0 deletions boilerplate/template/README.md.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# {{{ name }}}

[![NPM version](https://img.shields.io/npm/v/{{{ name }}}.svg?style=flat)](https://npmjs.org/package/{{{ name }}})
[![NPM downloads](http://img.shields.io/npm/dm/{{{ name }}}.svg?style=flat)](https://npmjs.org/package/{{{ name }}})

## Install

```bash
$ {{ npmClient }} install
```

```bash
$ npm run dev
$ npm run build
```

## Options

TODO

## LICENSE

MIT
28 changes: 28 additions & 0 deletions boilerplate/template/package.json.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "{{{ name }}}",
"version": "0.0.1",
"description": "{{{ description }}}",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"dev": "father dev",
"build": "father build",
"build:deps": "father prebundle",
"prepublishOnly": "npm run build"
},
"keywords": [],
"authors": [{{#author}}
"{{{ author }}}"
{{/author}}],
"license": "MIT",
"files": [
"dist",
"compiled"
],
"publishConfig": {
"access": "public"
},
"devDependencies": {
"father": "{{{ version }}}"
}
}
1 change: 1 addition & 0 deletions boilerplate/template/src/index.ts.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'Hello father 4!';
8 changes: 8 additions & 0 deletions boilerplate/template/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"strict": true,
"declaration": true,
"skipLibCheck": true,
"baseUrl": "./"
}
}
8 changes: 8 additions & 0 deletions boilerplate/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src"]
}
8 changes: 4 additions & 4 deletions examples/normal/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"scripts": {
"build": "../../bin/father.js build",
"build:no-clean": "../../bin/father.js build --no-clean",
"version": "../../bin/father.js version"
"build": "father build",
"build:no-clean": "father build --no-clean",
"version": "father version"
},
"dependencies": {
"father": "link:..\\.."
"father": "workspace:*"
}
}
3 changes: 2 additions & 1 deletion examples/prebundle/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"scripts": {
"build:deps": "../../bin/father.js prebundle"
"build:deps": "father prebundle"
},
"devDependencies": {
"@types/minimatch": "3.0.5",
"father": "workspace:*",
"minimatch": "3.1.2",
"rimraf": "3.0.2"
}
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@
],
"scripts": {
"build": "pnpm tsc",
"build:release": "pnpm tsc -p tsconfig.build.json",
"clean": "rimraf dist",
"dev": "pnpm build --watch",
"format": "prettier --write .",
"prepare": "husky install",
"prepublish": "pnpm clean && pnpm build:release",
"test": "jest",
"test:cov": "jest --collectCoverage"
},
Expand Down
Loading

0 comments on commit 6010dbf

Please sign in to comment.