Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiple source #15

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest All",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": ["--runInBand", "--watch"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
}
},
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": [
"${fileBasenameNoExtension}",
"--config",
"jest.config.js"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
}
}
]
}
2 changes: 2 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const stream = majo()

## stream.source(globs, [options])

May be used multiple times before processing, but will simply console warn if multiple files under the same name are found returning only the last found.

### globs

Type: `Array` `string`<br>
Expand Down
44 changes: 31 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class Majo extends EventEmitter {
* @type {{[k: string]: any}}
*/
this.meta = {}
this.baseDir = []
this.sourcePatterns = []
this.dotFiles = []
}

/**
Expand All @@ -26,9 +29,10 @@ class Majo extends EventEmitter {
* @param opts.dotFiles Including dot files
*/
source(patterns, { baseDir = '.', dotFiles = true } = {}) {
this.baseDir = path.resolve(baseDir)
this.sourcePatterns = patterns
this.dotFiles = dotFiles
this.currentDir = path.resolve('.')
this.baseDir.push(path.resolve(baseDir))
this.sourcePatterns.push(patterns)
this.dotFiles.push(dotFiles)
return this
}

Expand All @@ -45,11 +49,22 @@ class Majo extends EventEmitter {
* Process middlewares against files
*/
async process() {
const allStats = await glob(this.sourcePatterns, {
cwd: this.baseDir,
dot: this.dotFiles,
stats: true
})
const { baseDir, dotFiles, sourcePatterns } = this
const allStats = await Promise.all(sourcePatterns.map((x, i) => {
return glob(x, {
cwd: baseDir[i],
dot: dotFiles[i],
stats: true
})
}))

const flattened = allStats.reduce((a, set, i) => {
set.forEach(x => {
x.absPath = path.resolve(baseDir[i], x.path)
a.push(x)
})
return a
}, [])

/**
* @typedef {{path: string, stats: fs.Stats, contents: Buffer}} File
Expand All @@ -58,10 +73,13 @@ class Majo extends EventEmitter {
this.files = {}

await Promise.all(
allStats.map(stats => {
const absolutePath = path.resolve(this.baseDir, stats.path)
return fs.readFile(absolutePath).then(contents => {
const file = { contents, stats, path: absolutePath }
flattened.map(stats => {
const { absPath } = stats
return fs.readFile(absPath).then(contents => {
const file = { contents, stats, path: absPath }
if (typeof this.files[stats.path] !== 'undefined') {
console.warn('majo has encountered duplicate relative path and has overwritten initial one due duplicate sources for: ' + stats.path)
}
this.files[stats.path] = file
})
})
Expand Down Expand Up @@ -199,7 +217,7 @@ class Majo extends EventEmitter {
rename(fromPath, toPath) {
const file = this.files[fromPath]
this.createFile(toPath, {
path: path.resolve(this.baseDir, toPath),
path: file.path,
stats: file.stats,
contents: file.contents
})
Expand Down
Empty file.
23 changes: 23 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,26 @@ test('rename', async () => {

expect(stream.fileList).toEqual(['b/c.txt'])
})

test('double source', async () => {
const stream = majo()

stream.source('**/*.md', { baseDir: path.join(__dirname, 'fixture/doubleSource') })
stream.source('**/*.md', { baseDir: path.join(__dirname, 'fixture/stats') })

await stream.process()

expect(stream.files['bar.md']).toBeDefined()
expect(stream.files['foo.md']).toBeDefined()
})

test.skip('duplicates not overwritten with double source', async () => {
const stream = majo()

stream.source('**/foo.md', { baseDir: path.join(__dirname, 'fixture/doubleSource') })
stream.source('**/foo.md', { baseDir: path.join(__dirname, 'fixture/stats') })

await stream.process()

// foo from different dirs
})