ソースを参照

chore: auto create pkg declare

famanoder 6 年 前
コミット
68cdf19842
4 ファイル変更100 行追加18 行削除
  1. 6 5
      scripts/createCptTpl.js
  2. 83 0
      scripts/createPkgDeclare.js
  3. 0 1
      src/nutui.js
  4. 11 12
      types/nutui.d.ts

+ 6 - 5
scripts/createCptTpl.js

@@ -1,9 +1,9 @@
 const conf = require('../src/config.json');
 const conf = require('../src/config.json');
-const readline = require('readline');
 const fs = require('fs');
 const fs = require('fs');
 const path = require('path');
 const path = require('path');
 const inquirer = require('inquirer');
 const inquirer = require('inquirer');
 const copy = require('copy');
 const copy = require('copy');
+const createPkgDeclare = require('./createPkgDeclare');
 
 
 let sorts = [...conf.sorts];
 let sorts = [...conf.sorts];
 
 
@@ -110,7 +110,7 @@ Vue.${newCpt.type}(${newCpt.name}.name, ${newCpt.name});
 
 
 export default ${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`);
         const filePath = path.join(dirPath, `index.js`);
         if (!fs.existsSync(dirPath)) {
         if (!fs.existsSync(dirPath)) {
             fs.mkdirSync(dirPath);
             fs.mkdirSync(dirPath);
@@ -143,7 +143,7 @@ export default {
     }
     }
 }
 }
 </script>`;
 </script>`;
-        const dirPath = path.join(__dirname, `../src/packages/${nameLc}/`);
+        const dirPath = path.join(__dirname, `../src/packages/${nameLc}`);
         const filePath = path.join(dirPath, `${nameLc}.vue`);
         const filePath = path.join(dirPath, `${nameLc}.vue`);
         if (!fs.existsSync(dirPath)) {
         if (!fs.existsSync(dirPath)) {
             fs.mkdirSync(dirPath);
             fs.mkdirSync(dirPath);
@@ -161,7 +161,7 @@ function createScss() {
         let content = `.nut-${nameLc}{
         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`);
         const filePath = path.join(dirPath, `${nameLc}.scss`);
         if (!fs.existsSync(dirPath)) {
         if (!fs.existsSync(dirPath)) {
             fs.mkdirSync(dirPath);
             fs.mkdirSync(dirPath);
@@ -175,7 +175,7 @@ function createScss() {
 
 
 function createDir() {
 function createDir() {
     const nameLc = newCpt.name.toLowerCase();
     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)) {
     if (!fs.existsSync(destPath)) {
         fs.mkdirSync(destPath);
         fs.mkdirSync(destPath);
     }
     }
@@ -211,6 +211,7 @@ function createNew() {
     }).then(() => {
     }).then(() => {
         return addToPackageJson();
         return addToPackageJson();
     }).then(() => {
     }).then(() => {
+        createPkgDeclare(newCpt.name);
         console.log('组件模板生成完毕,请开始你的表演~');
         console.log('组件模板生成完毕,请开始你的表演~');
         process.exit();
         process.exit();
     });
     });

+ 83 - 0
scripts/createPkgDeclare.js

@@ -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;

+ 0 - 1
src/nutui.js

@@ -2,7 +2,6 @@ import { version } from '../package.json';
 import { packages as pkgList } from './config.json';
 import { packages as pkgList } from './config.json';
 import { locale } from './locales';
 import { locale } from './locales';
 
 
-
 import Cell from './packages/cell/index.js';
 import Cell from './packages/cell/index.js';
 import './packages/cell/cell.scss';
 import './packages/cell/cell.scss';
 import Dialog from './packages/dialog/index.js';
 import Dialog from './packages/dialog/index.js';

+ 11 - 12
types/nutui.d.ts

@@ -4,6 +4,17 @@ declare class UIComponent extends Vue {
     static install (vue: typeof Vue): void
     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 ActionSheet extends UIComponent {}
 export declare class Badge extends UIComponent {}
 export declare class Badge extends UIComponent {}
 export declare class Button extends UIComponent {}
 export declare class Button extends UIComponent {}
@@ -47,15 +58,3 @@ export declare class Toast extends UIComponent {}
 export declare class BackTop extends UIComponent {}
 export declare class BackTop extends UIComponent {}
 export declare class Scroller extends UIComponent {}
 export declare class Scroller extends UIComponent {}
 export declare class CountDown 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
-