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

Implements #364 #365

Open
wants to merge 3 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
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ emitter.clone('path/to/dest').then(() => {

## Actions

You can manipulate repositories after they have been cloned with _actions_, specified in a `degit.json` file that lives at the top level of the working directory. Currently, there are two actions — `clone` and `remove`. Additional actions may be added in future.
You can manipulate repositories after they have been cloned with _actions_, specified in a `degit.json` file that lives at the top level of the working directory. Currently, there are three actions — `clone`, `remove`, and `search_replace`. Additional actions may be added in future.

### clone

Expand Down Expand Up @@ -164,6 +164,26 @@ This will clone `user/another-repo`, preserving the contents of the existing wor

Remove a file at the specified path.

### search_replace

```json
// degit.json
[
{
"action": "search_replace",
"files": [
"one.txt",
"two.json",
"three.mjs"
],
"pattern": "\\{\\{name\\}\\}",
"replacement": "PROJECT_NAME"
}
]
```

This will perform a search-and-replace in the given plaintext files. For example, this can be used to populate a project with an initial name, or various "author" references with email addresses, etc. The replacement regular expression is defined by the `pattern` field; matches in those files will be replaced by the value of the environmental variable identified by the `replacement` value in the user runtime. For example, the configuration listed above will replace all occurances of `{{name}}` in the given files with the value of the environmental variable `$PROJECT_NAME`.

## See also

- [zel](https://github.com/vutran/zel) by [Vu Tran](https://twitter.com/tranvu)
Expand Down
15 changes: 14 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import process from 'process';
import fs from 'fs';
import path from 'path';
import tar from 'tar';
Expand Down Expand Up @@ -70,7 +71,8 @@ class Degit extends EventEmitter {
process.exit(1);
});
},
remove: this.remove.bind(this)
remove: this.remove.bind(this),
search_replace: this.search_replace.bind(this)
};
}

Expand Down Expand Up @@ -158,6 +160,17 @@ class Degit extends EventEmitter {
}
}

search_replace(dir, dest, action) {
const re = new RegExp(action.pattern, 'g');
const value = process.env[action.replacement];
action.files.forEach(file => {
const file_path = `${dest}${path.sep}${file}`;
let contents = fs.readFileSync(file_path, "utf8");
contents = contents.replace(re, value);
fs.writeFileSync(file_path, contents);
});
}

_checkDirIsEmpty(dir) {
try {
const files = fs.readdirSync(dir);
Expand Down