Skip to content

Commit

Permalink
replace name within buffer in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Aug 9, 2019
1 parent 1033900 commit 93196e7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
16 changes: 14 additions & 2 deletions packages/moon-cli/dist/moon-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,24 @@
"User-Agent": "Node.js"
}
};
var MoonNameRE = /{# MoonName #}/g;

function log(type, message) {
console.log("\x1B[34m" + type + "\x1B[0m " + message);
}

function replace(content, sub, subNewString) {
var index = content.indexOf(sub);

if (index === -1) {
return content;
} else {
var left = content.slice(0, index);
var right = replace(content.slice(index + sub.length), sub, subNewString);
var subNew = Buffer.from(subNewString);
return Buffer.concat([left, subNew, right], left.length + subNew.length + right.length);
}
}

function download(res) {
var archivePath = path.join(__dirname, "moon-template.tar.gz");
var stream = fs.createWriteStream(archivePath);
Expand Down Expand Up @@ -77,7 +89,7 @@
if (fs.statSync(nextPath).isDirectory()) {
create(nextPath, targetPath);
} else {
fs.writeFileSync(nextPath, fs.readFileSync(nextPath).toString().replace(MoonNameRE, name));
fs.writeFileSync(nextPath, replace(fs.readFileSync(nextPath), "{# MoonName #}", name));
log("create", path.relative(targetPath, nextPath));
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/moon-cli/dist/moon-cli.min.js

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

19 changes: 15 additions & 4 deletions packages/moon-cli/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,24 @@ const archive = {
}
};

const MoonNameRE = /{# MoonName #}/g;

function log(type, message) {
console.log(`\x1b[34m${type}\x1b[0m ${message}`);
}

function replace(content, sub, subNewString) {
const index = content.indexOf(sub);

if (index === -1) {
return content;
} else {
const left = content.slice(0, index);
const right = replace(content.slice(index + sub.length), sub, subNewString);
const subNew = Buffer.from(subNewString);

return Buffer.concat([left, subNew, right], left.length + subNew.length + right.length);
}
}

function download(res) {
const archivePath = path.join(__dirname, "moon-template.tar.gz");
const stream = fs.createWriteStream(archivePath);
Expand Down Expand Up @@ -75,8 +87,7 @@ function create(currentPath, targetPath) {
if (fs.statSync(nextPath).isDirectory()) {
create(nextPath, targetPath);
} else {
fs.writeFileSync(nextPath, fs.readFileSync(nextPath).toString().replace(MoonNameRE, name));

fs.writeFileSync(nextPath, replace(fs.readFileSync(nextPath), "{# MoonName #}", name));
log("create", path.relative(targetPath, nextPath));
}
}
Expand Down

0 comments on commit 93196e7

Please sign in to comment.