Skip to content

Commit

Permalink
chore: auto create pkg declare
Browse files Browse the repository at this point in the history
  • Loading branch information
famanoder committed Apr 12, 2019
1 parent 30f6ac6 commit 68cdf19
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 18 deletions.
11 changes: 6 additions & 5 deletions scripts/createCptTpl.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const conf = require('../src/config.json');
const readline = require('readline');
const fs = require('fs');
const path = require('path');
const inquirer = require('inquirer');
const copy = require('copy');
const createPkgDeclare = require('./createPkgDeclare');

let sorts = [...conf.sorts];

Expand Down Expand Up @@ -110,7 +110,7 @@ Vue.${newCpt.type}(${newCpt.name}.name, ${newCpt.name});
export default ${newCpt.name}`;

const dirPath = path.join(__dirname, `../src/packages/${nameLc}/`);
const dirPath = path.join(__dirname, `../src/packages/${nameLc}`);
const filePath = path.join(dirPath, `index.js`);
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
Expand Down Expand Up @@ -143,7 +143,7 @@ export default {
}
}
</script>`;
const dirPath = path.join(__dirname, `../src/packages/${nameLc}/`);
const dirPath = path.join(__dirname, `../src/packages/${nameLc}`);
const filePath = path.join(dirPath, `${nameLc}.vue`);
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
Expand All @@ -161,7 +161,7 @@ function createScss() {
let content = `.nut-${nameLc}{
}`;
const dirPath = path.join(__dirname, `../src/packages/${nameLc}/`);
const dirPath = path.join(__dirname, `../src/packages/${nameLc}`);
const filePath = path.join(dirPath, `${nameLc}.scss`);
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
Expand All @@ -175,7 +175,7 @@ function createScss() {

function createDir() {
const nameLc = newCpt.name.toLowerCase();
const destPath = path.join(__dirname, '../src/packages/' + nameLc + '/');
const destPath = path.join(__dirname, '../src/packages/' + nameLc);
if (!fs.existsSync(destPath)) {
fs.mkdirSync(destPath);
}
Expand Down Expand Up @@ -211,6 +211,7 @@ function createNew() {
}).then(() => {
return addToPackageJson();
}).then(() => {
createPkgDeclare(newCpt.name);
console.log('组件模板生成完毕,请开始你的表演~');
process.exit();
});
Expand Down
83 changes: 83 additions & 0 deletions scripts/createPkgDeclare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const fs = require('fs');
const path = require('path');
const t = require('@babel/types');
const {parse} = require('@babel/parser');
const {default: traverse} = require('@babel/traverse');
const {default: generate} = require('@babel/generator');

const PKGS = 'packages';
const emptyLine = '/*hr*/';
const nutMainFile = path.join(__dirname, '../src/nutui.js');
const nutTypings = path.join(__dirname, '../types/nutui.d.ts');

function transformCodes(codes, visitor) {
const ast = parse(codes, {
sourceType: "module"
});
traverse(ast, visitor);
const {code} = generate(ast, { /* options */ }, codes);
return code;
}

function insertImports(pkg) {
const lowername = pkg.toLowerCase();
this.insertBefore(
t.importDeclaration([
t.importDefaultSpecifier(t.identifier(pkg))
],
t.stringLiteral(`./packages/${lowername}/index.js`))
);
this.insertBefore(
t.importDeclaration([], t.stringLiteral(`./packages/${lowername}/${lowername}.scss`))
);
this.insertBefore(t.stringLiteral(emptyLine));
}

function createProp(pkg) {
return t.objectProperty(t.identifier(pkg), t.identifier(pkg));
}

function addToExport(pkg, init) {
init.properties.push(createProp(pkg));
this.replaceWith(t.variableDeclaration('const', [
t.variableDeclarator(t.identifier(PKGS), init)
]));
}

function addPkgDeclare(pkg) {
const codes = fs.readFileSync(nutMainFile).toString();
const visitor = {
VariableDeclaration: function(p) {
const {node} = p;//console.log(Object.keys(p.__proto__))
if(node) {
const {declarations = []} = node;
if(declarations.length) {
for(const {id, init} of declarations) {

if(id.name === PKGS && init.properties && init.properties.length) {
const props = init.properties.filter(({key}) => key.name.toLowerCase() === pkg.toLowerCase());
if(!props.length) {
insertImports.call(p, pkg);
addToExport.call(p, pkg, init);
p.insertAfter(t.stringLiteral(emptyLine));
p.stop();
break;
}
}
}
}
}
}
}

const code = transformCodes(codes, visitor);
return code;
}

function createPkgDeclare(pkg) {
const code = addPkgDeclare(pkg);
fs.writeFileSync(nutMainFile, code.replace(/"\/\*hr\*\/"/g, ''));
fs.appendFileSync(nutTypings, `export declare class ${pkg} extends UIComponent {}\n`);
}

module.exports = createPkgDeclare;
1 change: 0 additions & 1 deletion src/nutui.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { version } from '../package.json';
import { packages as pkgList } from './config.json';
import { locale } from './locales';


import Cell from './packages/cell/index.js';
import './packages/cell/cell.scss';
import Dialog from './packages/dialog/index.js';
Expand Down
23 changes: 11 additions & 12 deletions types/nutui.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ declare class UIComponent extends Vue {
static install (vue: typeof Vue): void
}

export interface InstallationOptions {
locale?: any
lang?: any
}

export const version: string

export const locale: (l:any) => void

export function install (vue: typeof Vue, options: InstallationOptions): void

export declare class ActionSheet extends UIComponent {}
export declare class Badge extends UIComponent {}
export declare class Button extends UIComponent {}
Expand Down Expand Up @@ -47,15 +58,3 @@ export declare class Toast extends UIComponent {}
export declare class BackTop extends UIComponent {}
export declare class Scroller extends UIComponent {}
export declare class CountDown extends UIComponent {}

export interface InstallationOptions {
locale?: any
lang?: any
}

export const version: string

export const locale: (l:any) => void

export function install (vue: typeof Vue, options: InstallationOptions): void

0 comments on commit 68cdf19

Please sign in to comment.