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 0de584f
Show file tree
Hide file tree
Showing 2 changed files with 46 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;
},
renameSubDirectoriesFn: (directoryName, templateName, _utils) => {
const { changeCase } = _utils;
const newDirectoryName = changeCase.paramCase(templateName);
return directoryName.replace(/__templateName__/g, newDirectoryName);
}
};
50 changes: 37 additions & 13 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,54 @@ async function replaceTextInFiles(
filePath,
templateName,
replaceFileTextFn,
replaceFileNameFn
renameFileFn,
renameSubDirectoriesFn
) {
try {
const stat = await fs.stat(filePath);
if (stat.isDirectory()) {
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,
renameSubDirectoriesFn
)
)
})
);
<<<<<<< HEAD
if (typeof renameSubDirectoriesFn === 'function') {
const currDirectoryName = path.basename(filePath);
const newDirectoryName = renameSubDirectoriesFn(currDirectoryName, templateName, {
=======
if (typeof renameSubDirectroriesFn === 'function') {
const currDirectoryName = path.basename(filePath);
const newDirectoryName = renameSubDirectroriesFn(currDirectoryName, templateName, {
>>>>>>> 4443808... fix: changes to `template.config.js` are immediately reflected in the extension
changeCase,
path
});
const newPath = path.resolve(filePath, '../', newDirectoryName);
fs.renameSync(filePath, newPath);
}
} else {
const fileText = (await fs.readFile(filePath)).toString("utf8");
if (typeof replaceFileTextFn === "function") {
await fs.writeFile(
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 @@ -98,6 +114,10 @@ async function createNew(_context, isRenameTemplate) {
await makeTemplateConfigJs(configFilePath);
}

/**
* Clear the `template.config.js` cache from `require`
*/
delete require.cache[configFilePath];
const config = require(configFilePath);
const templateRootPath = path.resolve(
workspaceRootPath,
Expand Down Expand Up @@ -132,9 +152,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 +163,11 @@ async function createNew(_context, isRenameTemplate) {
dstPath,
dstTemplateName,
config.replaceFileTextFn,
config.replaceFileNameFn
/**
* @deprecated `replaceFileNameFn` is deprecated, using `renameFileFn`
*/
config.replaceFileNameFn && config.renameFileFn,
config.renameSubDirectoriesFn
);
vscode.window.showInformationMessage("Template: copied!");
} catch (e) {
Expand Down Expand Up @@ -173,7 +197,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 0de584f

Please sign in to comment.