Skip to content

Commit

Permalink
perf(edam): template config append process
Browse files Browse the repository at this point in the history
  • Loading branch information
imcuttle committed Jun 27, 2018
1 parent ad6b12c commit 03d8253
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 26 deletions.
14 changes: 9 additions & 5 deletions packages/edam/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/edam/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"untildify": "^3.0.2",
"update-notifier": "^2.4.0",
"url-join": "^4.0.0",
"walli": "0.0.5"
"walli": "^1.1.2"
},
"devDependencies": {
"bdd-stdin": "^0.2.0"
Expand Down
71 changes: 71 additions & 0 deletions packages/edam/src/__tests__/edam/process_config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* eslint-disable quotes */
/**
* @file spec
* @author Cuttle Cong
* @date 2018/3/28
* @description
*/
import { mockPrompts } from '../../index'
import { join, relative, normalize } from 'path'
import fileSystem from '../../lib/fileSystem'

async function readdirDeep(dest) {
const files = await fileSystem.readdirDeep(dest)
return files.filter(x => fileSystem.isFile(x)).map(x => relative(dest, x))
}
describe('process_config', function() {
const tplPath = join(__dirname, '../fixture/edam')
const outputRoot = join(__dirname, '../fixture/edam-output')

it('should process_config', async () => {
const ft = await mockPrompts(
join(tplPath, 'process_config'),
{
value: 'ojbk'
},
join(outputRoot, 'process_config')
)

expect(Object.keys(ft.tree)).toEqual(
expect.arrayContaining([
normalize('.gitignore'),
normalize('imignored/keep.module.js'),
normalize('index.js')
])
)
})

it('should process_config output', async function() {
const fp = await mockPrompts(
join(tplPath, 'process_config'),
{
// value: 'ojbk'
},
join(outputRoot, 'process_config')
)

expect(Object.keys(fp.tree)).toEqual(
expect.arrayContaining([
normalize('.gitignore'),
normalize('imignored/keep.module.js'),
normalize('index.js')
])
)
expect(await fp.writeToFile(null, { overwrite: true })).toBeTruthy()

expect(
await readdirDeep(join(outputRoot, 'process_config'))
).toEqual(
expect.arrayContaining([
normalize('.gitignore'),
normalize('imignored/keep.module.js'),
normalize('index.js')
])
)
expect(
require(join(outputRoot, 'process_config', 'index.js'))
).toBe('process_config')

await fileSystem.cleanDir(join(outputRoot, 'process_config'))
})
})
42 changes: 42 additions & 0 deletions packages/edam/src/__tests__/fixture/edam/process_config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @file index
* @author Cuttle Cong
* @date 2018/3/28
* @description
*/

module.exports = function(/*options*/) {
return {
prompts: [
{
name: 'username',
type: 'input',
default: '${git.name}'
},
{
name: 'value',
type: 'input',
default: '${baseName}'
}
],
process(answer) {
console.log('answer.username', answer.username)
return {
ignore: ['im_ignored', 'imignored/*', '!imignored/keep*'],
variables: {
haha: 'hhhh'
},
hooks: {
assets: [
function(assets) {
console.log(assets)
}
]
},
usefulHook: {
gitInit: true
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= JSON.stringify(_.file) %>
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @loader module?indent=4

module.exports = function (variables) {
return Object.assign({}, {
name: variables.value,
version: '2.3.5'
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @file index
* @author <%= JSON.stringify(_.file) %>
* @date 2018/3/28
* @description
*/

module.exports = '<%= value %>'
6 changes: 6 additions & 0 deletions packages/edam/src/core/plugins/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ export default async function normalize(
) {
await this.emit('normalize:templateConfig:before', templateConfig)
const data = await this.compiler.variables.get()

if (typeof templateConfig.process === 'function') {
const processedConfig = await templateConfig.process(data)
Object.assign(templateConfig, processedConfig)
}

templateConfig.root = await dynamicGet<string>(templateConfig.root, [data])
if (!templateConfig.root) {
templateConfig.root = './template'
Expand Down
40 changes: 20 additions & 20 deletions packages/edam/src/types/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
oneOf,
string,
boolean,
object,
array,
objectOf,
arrayOf,
eq,
function_,
any,
Expand All @@ -22,9 +22,9 @@ const strictSource = createVerifiableClass({
_check(req: any) {
return leq({
type: oneOf(['file', 'git', 'npm']),
url: string(),
checkout: string().optional(),
version: string().optional()
url: string,
checkout: string.optional,
version: string.optional
}).check(req)
},
getDisplayName() {
Expand All @@ -34,33 +34,33 @@ const strictSource = createVerifiableClass({

const source = createVerifiableClass({
_check(req: any) {
return oneOf([strictSource(), string()]).check(req)
return oneOf([strictSource(), string]).check(req)
},
getDisplayName() {
return 'source'
}
})

export const rc: LooseEqual = leq({
source: source().optional(),
cacheDir: oneOf([boolean(), string()]).optional(),
alias: object(source()).optional(),
extends: oneOf([string(), array(string())]).optional(),
output: string().optional(),
plugins: array(eq([function_(), any()])).optional(),
source: source().optional,
cacheDir: oneOf([boolean, string]).optional,
alias: objectOf(source()).optional,
extends: oneOf([string, arrayOf(string)]).optional,
output: string.optional,
plugins: arrayOf(eq([function_, any])).optional,
pull: leq({
npmClient: oneOf(['yarn', 'npm']).optional(),
git: oneOf(['clone', 'download']).optional()
}).optional(),
storePrompts: boolean().optional()
npmClient: oneOf(['yarn', 'npm']).optional,
git: oneOf(['clone', 'download']).optional
}).optional,
storePrompts: boolean.optional
})

export const edam = rc.assign(
leq({
name: string().optional(),
updateNotify: boolean().optional(),
yes: boolean().optional(),
silent: boolean().optional()
name: string.optional,
updateNotify: boolean.optional,
yes: boolean.optional,
silent: boolean.optional
})
)

Expand Down
11 changes: 11 additions & 0 deletions packages/edam/src/types/TemplateConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export type AsyncOrSync<T> = Promise<T> | T
export type PromptType = 'checkbox' | 'radio' | 'input' | 'suggest'

import * as w from 'walli'

export interface Prompt {
message: string
default?: any
Expand Down Expand Up @@ -41,9 +43,18 @@ export type Mapper = {

export type Dynamic<T> = (answer: object) => T | Promise<T>

// export const templateConfigType = w.leq({
//
// })

export default interface TemplateConfig {
prompts?: Array<Prompt>

/**
* answers => ({ hooks, ignore, ...(exclude prompts) })
*/
process?: Function

hooks?: {
[hookName: string]: Array<Hook> | Hook
} | Dynamic<object>
Expand Down

0 comments on commit 03d8253

Please sign in to comment.