Skip to content

Commit

Permalink
fix(edam): normalize source and alias which are relative path
Browse files Browse the repository at this point in the history
  • Loading branch information
imcuttle committed Jun 1, 2018
1 parent 22ffe67 commit 8e7d546
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 24 deletions.
3 changes: 3 additions & 0 deletions packages/edam-cli/bin/__tests__/.edamrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"source": "./fixture"
}
Empty file.
43 changes: 32 additions & 11 deletions packages/edam-cli/bin/__tests__/main.spec.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,30 @@
* @date 2018/3/31
* @description
*/
process.env.DEBUG = 'edam:*'
const spawn = require('cross-spawn')
const { join } = require('path')
const fs = require('fs')
const rimraf = require('rimraf')

const cwd = join(__dirname, '..')
const gCwd = join(__dirname, '..')

const exec = function(argString) {
const sp = spawn.sync('./edam.js', argString.split(' '), { cwd })
const exec = function(argString, { cwd = gCwd } = {}) {
const sp = spawn.sync(join(__dirname, '../edam.js'), argString.split(' '), {
cwd
})
console.log('arguments', argString)
console.log('sp.stderr', sp.stderr.toString())
console.log('sp.stderr', sp.stderr && sp.stderr.toString())
console.log('sp.error', sp.error)
return sp
}

describe('main.spec', function() {

afterEach(function () {
rimraf.sync(join(__dirname, 'fixture/output'))
})

it('should `--help` flag works', async () => {
const sp = exec('--help')
expect(sp.stdout.toString()).toEqual(expect.stringContaining('Usage'))
Expand All @@ -29,15 +38,27 @@ describe('main.spec', function() {
})

it('should `--cache-dir` flag works', async () => {
const sp = exec('./__tests__/fixture -o ./__tests__/fixture/output -y -w --cache-dir=' + join(__dirname, '.cache'))
const sp = exec(
'./__tests__/fixture -o ./__tests__/fixture/output -y -w --cache-dir=' +
join(__dirname, '.cache')
)

expect(
fs.readFileSync(join(cwd, './__tests__/fixture/output/template.txt')).toString()
).toBe([
'user: edam-user\n' +
'pass: edam-user\n' +
'age: null\n'
].join(''))
fs
.readFileSync(join(gCwd, './__tests__/fixture/output/template.txt'))
.toString()
).toBe(['user: edam-user\n' + 'pass: edam-user\n' + 'age: null\n'].join(''))
})

it('should works fine when source is setting in parent', async () => {
const sp = exec('-o ../fixture/output -y -w', {
cwd: join(__dirname, 'cwd')
})

expect(
fs
.readFileSync(join(gCwd, './__tests__/fixture/output/template.txt'))
.toString()
).toBe(['user: edam-user\n' + 'pass: edam-user\n' + 'age: null\n'].join(''))
})
})
7 changes: 3 additions & 4 deletions packages/edam-cli/bin/edam.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,15 @@ ${generateFlagHelp(flags, ' ')}
}
)

// console.log(flags.silent)
const edam = require('edam').default
const Edam = require('edam').Edam
// default
if (config.cacheDir === tildify(Edam.constants.DEFAULT_CACHE_DIR)) {
config.cacheDir = Edam.constants.DEFAULT_CACHE_DIR
}
// if (config.output === tildify(process.cwd())) {
// config.output = process.cwd()
// }
if (config.output === '') {
delete config.output
}


let spinner = require('ora')()
Expand Down
27 changes: 19 additions & 8 deletions packages/edam/src/core/extendsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ export type Track = {
value?: any
}
}

function toNormalizedFileSource(source, options) {
if (!source) {
return source
}
let s = normalizeSource(source, options)
if (s.type === 'file') {
return s.url
}
return source
}

export async function innerExtendsConfig(
config: EdamConfig,
options: Options,
Expand All @@ -35,14 +47,13 @@ export async function innerExtendsConfig(
config.output = nps.resolve(options.cwd, config.output)
}

// Ignore normalize source here
// maybe is alias
// if (config.source) {
// let s = normalizeSource(config.source, options)
// if (s.type === 'file') {
// config.source = s.url
// }
// }
// source and alias could be relative path
config.source = toNormalizedFileSource(config.source, options)
if (config.alias) {
_.each(config.alias, (val, key) => {
config.alias[key] = toNormalizedFileSource(val, options)
})
}

if (config.plugins) {
const plugins = (config.plugins = toArray(config.plugins))
Expand Down
2 changes: 1 addition & 1 deletion packages/edam/src/core/normalizeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @date 2018/3/23
* @description
*/
import { edam as edamType, EdamConfig } from '../types/Options'
import { EdamConfig } from '../types/Options'
import { default as normalizeSource, Options } from './normalizeSource'
import { load } from '../lib/loadConfig'
import extendsMerge from './extendsMerge'
Expand Down

0 comments on commit 8e7d546

Please sign in to comment.