-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add command "eject" to override default files in the current pr…
…oject
- Loading branch information
Showing
12 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* eslint-disable no-console */ | ||
|
||
const customize = require('customize') | ||
const debug = require('debug')('though:eject') | ||
const fs = require('fs-extra') | ||
const path = require('path') | ||
|
||
module.exports = async function eject(optionalPrefix, filename) { | ||
if (filename == null) { | ||
return logEjectableFiles(optionalPrefix) | ||
} | ||
|
||
const config = await customize() | ||
// Load `customize`-spec | ||
.load(require('../customize')('.')) | ||
.buildConfig() | ||
|
||
switch (optionalPrefix) { | ||
case 'template': | ||
return ejectFile({ | ||
source: config.handlebars.templates[filename], | ||
targetFile: path.join('.thought', 'templates', filename), | ||
notFoundMessage: `There is no template "${filename}"!` | ||
}) | ||
case 'partial': | ||
return ejectFile({ | ||
source: config.handlebars.partials[filename], | ||
targetFile: path.join('.thought', 'partials', filename), | ||
notFoundMessage: `There is no partial "${filename}"!` | ||
}) | ||
} | ||
} | ||
|
||
async function logEjectableFiles(optionalPrefix) { | ||
const config = await customize() | ||
// Load `customize`-spec | ||
.load(require('../customize')('.')) | ||
.buildConfig() | ||
|
||
debug(config) | ||
|
||
console.log('I can eject the following files for you: ') | ||
switch (optionalPrefix) { | ||
case undefined: | ||
case null: | ||
logDefaultFiles(config.handlebars.templates, { prefix: 'template' }) | ||
logDefaultFiles(config.handlebars.partials, { prefix: 'partial' }) | ||
break | ||
case 'template': | ||
logDefaultFiles(config.handlebars.templates, { prefix: 'template' }) | ||
break | ||
case 'partial': | ||
logDefaultFiles(config.handlebars.partials, { prefix: 'partial' }) | ||
break | ||
default: | ||
throw new Error(`Unknown prefix "${optionalPrefix}", try without prefix!"`) | ||
} | ||
} | ||
|
||
function logDefaultFiles(fileObject, { prefix }) { | ||
Object.entries(fileObject).forEach(([fileName, file]) => { | ||
if (!isOverridden(file)) { | ||
console.log(` ${prefix} ${fileName}`) | ||
} | ||
}) | ||
} | ||
|
||
async function ejectFile({ source, targetFile, notFoundMessage }) { | ||
if (source == null) { | ||
throw new Error(notFoundMessage) | ||
} | ||
if (isOverridden(source)) { | ||
throw new Error(`File "${source.path}" already exists in this project!`) | ||
} | ||
|
||
console.log(`Ejecting "${targetFile}"`) | ||
await fs.copy(source.path, targetFile) | ||
} | ||
|
||
function isOverridden(file) { | ||
return file.path.match(/^\.thought/) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
test/fixtures/scenarios/with-partial-and-template/expected/.thought/partials/usage.md.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Overridden usage file |
1 change: 1 addition & 0 deletions
1
...xtures/scenarios/with-partial-and-template/expected/.thought/templates/anotherFile.md.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
this is another file |
27 changes: 27 additions & 0 deletions
27
test/fixtures/scenarios/with-partial-and-template/expected/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# simple-project | ||
|
||
[![NPM version](https://img.shields.io/npm/v/simple-project.svg)](https://npmjs.com/package/simple-project) | ||
|
||
> A simple description | ||
|
||
# Installation | ||
|
||
``` | ||
npm install simple-project | ||
``` | ||
|
||
Overridden usage file | ||
|
||
|
||
# License | ||
|
||
`simple-project` is published under the ISC-license. | ||
|
||
No file "LICENSE*" found | ||
|
||
|
||
|
||
# Contributing guidelines | ||
|
||
See [CONTRIBUTING.md](CONTRIBUTING.md). |
7 changes: 7 additions & 0 deletions
7
test/fixtures/scenarios/with-partial-and-template/expected/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* Beschreibung | ||
* @param {string=} param | ||
*/ | ||
function abc(param) { | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
test/fixtures/scenarios/with-partial-and-template/expected/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "eject-project", | ||
"version": "1.0.0", | ||
"description": "A simple description", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/unit-test/eject-project.git" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"fs-walker": "^1.0.0" | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
test/fixtures/scenarios/with-partial-and-template/input/.thought/partials/usage.md.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Overridden usage file |
1 change: 1 addition & 0 deletions
1
.../fixtures/scenarios/with-partial-and-template/input/.thought/templates/anotherFile.md.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
this is another file |
7 changes: 7 additions & 0 deletions
7
test/fixtures/scenarios/with-partial-and-template/input/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* Beschreibung | ||
* @param {string=} param | ||
*/ | ||
function abc(param) { | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
test/fixtures/scenarios/with-partial-and-template/input/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "eject-project", | ||
"version": "1.0.0", | ||
"description": "A simple description", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/unit-test/eject-project.git" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"fs-walker": "^1.0.0" | ||
} | ||
} |