Skip to content

Commit

Permalink
feat: first push
Browse files Browse the repository at this point in the history
  • Loading branch information
MriLiuJY committed Aug 11, 2022
0 parents commit a63c785
Show file tree
Hide file tree
Showing 21 changed files with 582 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules/**
lib/**
bin/**
.json
.eslintrc.js
29 changes: 29 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module.exports = {
env: {
browser: true,
es2021: true,
},
extends: [
'airbnb-base',
'plugin:import/errors',
'plugin:import/warnings',
'plugin:import/typescript',
],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 12,
sourceType: 'module',
},
plugins: [
'@typescript-eslint',
],
rules: {
'import/extensions': [
'error',
'ignorePackages',
{
ts: 'never',
},
],
},
};
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/npm-debug.log*
/yarn-error.log
/yarn.lock
/package-lock.json

# production
/dist

# misc
.DS_Store
.history

# umi
.umi
.umi-production
.umi-test
.env.local

# ide
/.vscode
/.idea
/pnpm-lock.yaml
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# one-cli
18 changes: 18 additions & 0 deletions bin/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#! /usr/bin/env node
const program = require('commander');
program.version(require('../package.json').version);
program
.command('init')
.description('init project ')
.action(() => {
// ---- 执行 lib 下的文件 ----
require("../lib/init")
})
program
.command('create')
.description('create pages')
.action(() => {
// ---- 执行 lib 下的文件 ----
require("../lib/create")
})
program.parse(process.argv)
2 changes: 2 additions & 0 deletions global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare module 'download-git-repo';
declare module '*.json';
50 changes: 50 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "developer-ones/onecli",
"version": "1.0.0",
"description": "developer-ones onecli cli",
"main": "/lib/commands/init/index.js",
"files": [
"lib/",
"bin/"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node lib/commands/create/index.js",
"build": "rimraf ./lib && rollup -c"
},
"keywords": [
"cli"
],
"author": "buyu",
"license": "ISC",
"devDependencies": {
"@rollup/plugin-commonjs": "^21.0.1",
"@rollup/plugin-eslint": "^8.0.1",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.1.3",
"@rollup/plugin-typescript": "^8.3.0",
"@types/fs-extra": "^9.0.13",
"@types/inquirer": "^8.2.0",
"@typescript-eslint/eslint-plugin": "^5.10.2",
"@typescript-eslint/parser": "^5.10.2",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-standard": "^16.0.3",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^5.2.0",
"rimraf": "^3.0.2",
"rollup": "^2.66.1",
"tslib": "^2.3.1",
"typescript": "^4.5.5"
},
"bin": {
"onecli": "./bin/init.js"
},
"dependencies": {
"chalk": "^4.1.2",
"commander": "^9.0.0",
"download-git-repo": "^3.0.2",
"fs-extra": "^10.0.0",
"inquirer": "^8.2.0"
}
}
19 changes: 19 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import json from '@rollup/plugin-json';
import typescript from '@rollup/plugin-typescript';
import commonjs from '@rollup/plugin-commonjs';
import pkg from './package.json';

export default {
input: ['src/commands/init.ts', 'src/commands/create.ts'],
output: {
dir: 'lib',
format: 'cjs',
exports: 'auto',
},
external: [...Object.keys(pkg.dependencies), 'fs', 'path'],
plugins: [
commonjs(),
json(),
typescript({ sourceMap: false }),
],
};
71 changes: 71 additions & 0 deletions src/commands/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import inquirer from 'inquirer';
import path from 'path';
import fs from 'fs-extra';
import config from '../config.json';
import { page, component, i18n } from '../template/index';

const question = [
// --- 模版 ---
{
type: 'list',
name: 'template',
choices: [
{ value: 'page', name: 'react page (react 项目中 Page 模版)' },
{ value: 'component', name: 'react component' },
{ value: 'i18n', name: 'i18n file (react 项目中多语言文件)' },
],
default: ['react'],
message: '选择将要创建的模块,选择 page 的时候会默认创建同名 i18n 文件',
},
// --- 名称 ---
{
type: 'input',
name: 'name',
message: 'init name',
default: 'test',
},
];

inquirer.prompt(question).then(async (answers) => {
let {
name,
template,
} = answers;
name.replace(name[0], name[0].toUpperCase());
/**
* 获取模版
*/
const getTemplate = (type: string) => {
switch (type) {
case 'page':
return page;
case 'component':
return component;
case 'i18n':
return i18n;
default:
return page;
}
};
/**
* 生成组件文件
*/
const buildCpsFiles = (type: string) => {
let fullPath = path.join(path.resolve(process.cwd(), (config as any).template[template]));
fullPath = type === 'i18n' ? fullPath : `${fullPath}/${name}`;
const fileList = getTemplate(type);
// 检查文件夹是否存在
fs.ensureDirSync(fullPath);
fileList.forEach((item) => {
const filePath = `${fullPath}/${item.name.replace('NAME', name)}`;
fs.outputFileSync(filePath, item.file(name));
});
};

buildCpsFiles(template);
// --- page 的时候默认创建 i18n ---
if (template === 'page') {
template = 'i18n';
buildCpsFiles('i18n');
}
});
62 changes: 62 additions & 0 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import inquirer from 'inquirer';
import config from '../config.json';
import downloadReport from '../utils/index';

const question = [
// --- 模版 ---
{
type: 'list',
name: 'template',
choices: [
{ value: 'react', name: 'react template (react 子应用模版)' },
{ value: 'npm', name: 'npm package (npm 包模版)' },
{ value: 'component', name: 'component template (单组件模版)' },
{ value: 'vite-react-ts', name: 'vite react-ts template (vite 模版)' },
{ value: 'node-ts', name: 'node Egg-ts template (egg TS 项目模版)' },
],
default: ['react'],
message: '选择将要初始化的模版',
},
// --- 名称 ---
{
type: 'input',
name: 'name',
message: 'Project name',
default: 'onecli',
},
// --- 新建仓库 ---
// {
// type: 'confirm', // yes or no 二选一
// name: 'needRemote',
// message: '是否创建新的远程仓库:',
// default: false
// },
// --- 描述 ---
{
type: 'input',
name: 'description',
message: 'Project description',
},
// --- 作者 ---
{
type: 'input',
name: 'author',
message: 'Author',
},
// --- 版本号 ---
{
type: 'input',
name: 'version',
message: 'version',
default: '1.0.0',
}];
inquirer.prompt(question).then(async (answers) => {
const {
name,
template,
description,
author,
version,
} = answers;
await downloadReport((config as any)[template].url, name, description, author, version);
});
7 changes: 7 additions & 0 deletions src/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"template": {
"page": "./src/Pages/",
"component": "./src/",
"i18n": "./src/i18n/"
}
}
52 changes: 52 additions & 0 deletions src/template/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import tplMd from './tpl-index.md';
import tplComponent from './tpl-component.tsx';
import tplIndex from './tpl-index.ts';
import tplCss from './tpl-index.less';
import tplPage from './tpl-page.tsx';
import tplStore from './tpl-store.ts';
import tplI18n from './tpl-i18n.js';

export const page = [
{
file: tplPage,
name: 'NAME.tsx',
},
{
file: tplCss,
name: 'index.less',
},
{
file: tplStore,
name: 'store.ts',
},
{
file: tplIndex,
name: 'index.ts',
},
];

export const component = [
{
file: tplComponent,
name: 'index.tsx',
},
{
file: tplMd,
name: 'index.md',
},
{
file: tplCss,
name: 'index.less',
},
];

export const i18n = [
{
file: tplI18n,
name: '/zh_CN/NAME.ts',
},
{
file: tplI18n,
name: '/en_US/NAME.ts',
},
];
18 changes: 18 additions & 0 deletions src/template/tpl-component.tsx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export default (pascalName: string) => `
import React from 'react';
import './index.less';
interface I${pascalName}Props {
}
const ${pascalName} = (props: I${pascalName}Props) => {
return (
<div className="${pascalName}">
</div>
)
}
export default ${pascalName};
`;
3 changes: 3 additions & 0 deletions src/template/tpl-i18n.js.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default () => `
export default {};
`;
1 change: 1 addition & 0 deletions src/template/tpl-index.less.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => '';
Loading

0 comments on commit a63c785

Please sign in to comment.