Skip to content

Commit

Permalink
feat: add rename sub-directory feature
Browse files Browse the repository at this point in the history
realted: #14
  • Loading branch information
stegano committed Feb 6, 2021
1 parent aeb8f1e commit 0cb333c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
15 changes: 9 additions & 6 deletions assets/template.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,24 @@ module.exports = {
const { changeCase } = utils;
// You can change the text in the file
return fileText
.replace(/__templateName__/gm, templateName)
.replace(/__templateName__/g, templateName)
.replace(
/__templateNameToPascalCase__/gm,
/__templateNameToPascalCase__/g,
changeCase.pascalCase(templateName)
)
.replace(
/__templateNameToParamCase__/gm,
/__templateNameToParamCase__/g,
changeCase.paramCase(templateName)
);
},
replaceFileNameFn: (fileName, templateName, utils) => {
renameFileFn: (fileName, _templateName, utils) => {
const { path } = utils;
// @see https://nodejs.org/api/path.html#path_path_parse_path
const { base } = path.parse(fileName);
// You can change the file name
return base;
},
renameSubDirectroriesFn: (directoryName, templateName, _utils) => {
const { changeCase } = _utils;
const newDirectoryName = changeCase.paramCase(templateName);
return directoryName.replace(/__templateName__/g, newDirectoryName);
}
};
40 changes: 27 additions & 13 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,32 @@ async function replaceTextInFiles(
filePath,
templateName,
replaceFileTextFn,
replaceFileNameFn
renameFileFn,
renameSubDirectroriesFn
) {
try {
const stat = await fs.stat(filePath);
if (stat.isDirectory()) {
if (typeof renameSubDirectroriesFn === 'function') {
const currDirectoryName = path.basename(filePath);
const newDirectoryName = renameSubDirectroriesFn(currDirectoryName, templateName, {
changeCase,
path
});
const newPath = path.resolve(filePath, '../', newDirectoryName);
fs.renameSync(filePath, newPath);
}
const files = await fs.readdir(filePath);
await Promise.all(
files.map(async entryFilePath =>
replaceTextInFiles(
files.map(async entryFilePath => {
return replaceTextInFiles(
path.resolve(filePath, entryFilePath),
templateName,
replaceFileTextFn,
replaceFileNameFn
renameFileFn,
renameSubDirectroriesFn
)
)
})
);
} else {
const fileText = (await fs.readFile(filePath)).toString("utf8");
Expand All @@ -43,15 +54,14 @@ async function replaceTextInFiles(
filePath,
replaceFileTextFn(fileText, templateName, { changeCase, path })
);

/**
* Rename file
* @ref https://github.com/stegano/vscode-template/issues/4
*/
if (typeof replaceFileNameFn === "function") {
if (typeof renameFileFn === "function") {
const filePathInfo = path.parse(filePath);
const { base: originalFilename } = filePathInfo;
const filename = replaceFileNameFn(originalFilename, templateName, {
const filename = renameFileFn(originalFilename, templateName, {
changeCase,
path
});
Expand Down Expand Up @@ -132,9 +142,9 @@ async function createNew(_context, isRenameTemplate) {
// Input template name from user
const dstTemplateName = isRenameTemplate
? await vscode.window.showInputBox({
prompt: "Input a template name",
value: templateName
})
prompt: "Input a template name",
value: templateName
})
: templateName;

const dstPath = path.resolve(workingPathDir, dstTemplateName);
Expand All @@ -143,7 +153,11 @@ async function createNew(_context, isRenameTemplate) {
dstPath,
dstTemplateName,
config.replaceFileTextFn,
config.replaceFileNameFn
/**
* @deprecated `replaceFileNameFn` is deprecated, using `renameFileFn`
*/
config.replaceFileNameFn && config.renameFileFn,
config.renameSubDirectroriesFn
);
vscode.window.showInformationMessage("Template: copied!");
} catch (e) {
Expand Down Expand Up @@ -173,7 +187,7 @@ function activate(context) {
exports.activate = activate;

// this method is called when your extension is deactivated
function deactivate() {}
function deactivate() { }

module.exports = {
activate,
Expand Down

0 comments on commit 0cb333c

Please sign in to comment.