Skip to content

Commit

Permalink
feat: use _ejs to identify template file (#451)
Browse files Browse the repository at this point in the history
* feat: use _ejs to identify template file

* fix: update ejsRender

* chore: match more accurate
  • Loading branch information
sspku-yqLiu authored Aug 21, 2020
1 parent d8ecb4c commit 2048497
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 37 deletions.
27 changes: 16 additions & 11 deletions packages/generate-material/src/ejsRenderDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import * as path from 'path';
import * as glob from 'glob';
import * as ejs from 'ejs';
import * as fse from 'fs-extra';
import { ITemplateOptions } from './type';
import { ITemplateOptions } from './index';

export default async function (dir: string, options: ITemplateOptions): Promise<void> {
return new Promise((resolve, reject) => {
glob(
'**/*.ejs',
'**/*.?(_)ejs',
{
cwd: dir,
nodir: true,
dot: true,
ignore: options.useEjsTemplate ? ['**/*.tsx.ejs', '**/*.jsx.ejs', 'node_modules/**'] : ['node_modules/**'],
ignore: ['node_modules/**'],
},
(err, files) => {
if (err) {
Expand All @@ -38,14 +38,19 @@ export default async function (dir: string, options: ITemplateOptions): Promise<

function renderFile(filepath: string, options: any): Promise<string> {
return new Promise((resolve, reject) => {
ejs.renderFile(filepath, options, (err, result) => {
if (err) {
return reject(err);
}

fse.removeSync(filepath);
fse.writeFileSync(filepath.replace(/\.ejs$/, ''), result);
if (filepath.endsWith('_ejs')) {
const templateFilePath = filepath.replace(/_ejs$/, 'ejs');
fse.renameSync(filepath, templateFilePath);
resolve();
});
} else {
ejs.renderFile(filepath, options, (err, result) => {
if (err) {
return reject(err);
}
fse.removeSync(filepath);
fse.writeFileSync(filepath.replace(/\.ejs$/, ''), result);
resolve();
});
}
});
}
2 changes: 1 addition & 1 deletion packages/generate-material/src/formatProject.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from 'path';
import * as fse from 'fs-extra';
import { isAliNpm } from 'ice-npm-utils';
import { ITemplateOptions } from './type';
import { ITemplateOptions } from './index';

interface IOptions {
rootDir: string;
Expand Down
25 changes: 24 additions & 1 deletion packages/generate-material/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,34 @@ import { getNpmTarball, getAndExtractTarball } from 'ice-npm-utils';

import ejsRenderDir from './ejsRenderDir';
import formatProject from './formatProject';
import { IOptions, ITemplateOptions } from './type';

/**
* init component by template
*/
export interface ITemplateOptions {
npmName: string; // @icedesign/ice-label
name?: string; // ice-label (english and variable)
kebabCaseName?: string; // ice-label
npmScope?: string; // @icedesign
title?: string; //
description?: string;
className?: string;
version?: string;
category?: string;
// web, miniapp...
projectTargets?: string[];
adaptor?: boolean;
}

export interface IOptions {
rootDir: string;
materialTemplateDir: string;
templateOptions: ITemplateOptions;
enablePegasus?: boolean;
enableDefPublish?: boolean;
materialType: 'component' | 'block' | 'scaffold';
}

export async function generateMaterial({
rootDir,
materialTemplateDir,
Expand Down
24 changes: 0 additions & 24 deletions packages/generate-material/src/type.ts

This file was deleted.

0 comments on commit 2048497

Please sign in to comment.