Skip to content

Commit

Permalink
feat: show error with button (create issue)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bajdzis committed Sep 15, 2019
1 parent 3a76894 commit 8d6bf32
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 60 deletions.
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "awesome-tree",
"displayName": "Awesome tree",
"description": "Stop creating folders, start creating structures! (Automated file creation)",
"version": "0.0.1",
"publisher": "bajdzis",
"version": "0.0.2",
"engines": {
"vscode": "^1.37.0"
},
Expand All @@ -13,6 +14,16 @@
"*"
],
"icon": "readme/awesome-tree-icon.png",
"keywords": [
"file",
"files",
"dir",
"directory",
"directories",
"tree",
"dirtree",
"filetree"
],
"repository": {
"type": "git",
"url": "https://github.com/Bajdzis/vscode-awesome-tree.git"
Expand Down
130 changes: 71 additions & 59 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,70 +19,82 @@ export function activate(context: vscode.ExtensionContext) {

const fileSystemWatcher = vscode.workspace.createFileSystemWatcher("**/*",false, true, true);
fileSystemWatcher.onDidCreate(async(uri: vscode.Uri) => {
const relative = getRelative(uri.path);

// when directory or file is not empty probably change name parent directory
if (isEmptyDirectory(uri)) {
const parentDir = path.dirname(uri.fsPath);
const newDirname = path.basename(uri.fsPath);
const infoAboutNewDirectory = getInfoAboutPath(relative);
const directories: Directories = {};

const directoryNames = fs.readdirSync(parentDir);
directoryNames.forEach(subDirectoryOrFiles => {
const subDirectoryOrFilesPath = path.resolve(parentDir, subDirectoryOrFiles);
if (fs.lstatSync(subDirectoryOrFilesPath).isDirectory() && newDirname !== subDirectoryOrFiles) {
const files = getAllFilesPath(subDirectoryOrFilesPath);
const directoryInfo = getInfoAboutPath(getRelative(subDirectoryOrFilesPath));
directories[subDirectoryOrFiles] = {
directoryInfo,
files:files.map((fileName) => ({
pathTemplate: createVariableTemplate(fileName.replace(subDirectoryOrFilesPath, ''),[directoryInfo]),
contentTemplate: createVariableTemplate(fs.readFileSync(fileName).toString(),[directoryInfo])
}))
};
try {
const relative = getRelative(uri.path);

// when directory or file is not empty probably change name parent directory
if (isEmptyDirectory(uri)) {
const parentDir = path.dirname(uri.fsPath);
const newDirname = path.basename(uri.fsPath);
const infoAboutNewDirectory = getInfoAboutPath(relative);
const directories: Directories = {};

const directoryNames = fs.readdirSync(parentDir);
directoryNames.forEach(subDirectoryOrFiles => {
const subDirectoryOrFilesPath = path.resolve(parentDir, subDirectoryOrFiles);
if (fs.lstatSync(subDirectoryOrFilesPath).isDirectory() && newDirname !== subDirectoryOrFiles) {
const files = getAllFilesPath(subDirectoryOrFilesPath);
const directoryInfo = getInfoAboutPath(getRelative(subDirectoryOrFilesPath));
directories[subDirectoryOrFiles] = {
directoryInfo,
files:files.map((fileName) => ({
pathTemplate: createVariableTemplate(fileName.replace(subDirectoryOrFilesPath, ''),[directoryInfo]),
contentTemplate: createVariableTemplate(fs.readFileSync(fileName).toString(),[directoryInfo])
}))
};
}
});

const preparePathFiles = Object.values(directories)
.reduce((files, data) => [
...files,
...data.files.map(file => file.pathTemplate).filter((file) => !files.includes(file))
], [] as string[]);

const answersQuestion = [
'Yes, generate files',
'No, thanks'
];

const resultQuestion = await vscode.window.showInformationMessage(
`Do you want to create ${preparePathFiles.length} file(s) in new "${newDirname}" folder?`,
...answersQuestion
);

if (resultQuestion !== answersQuestion[0]) {
return;
}
});

const preparePathFiles = Object.values(directories)
.reduce((files, data) => [
...files,
...data.files.map(file => file.pathTemplate).filter((file) => !files.includes(file))
], [] as string[]);

const answersQuestion = [
'Yes, generate files',
'No, thanks'
];

const resultQuestion = await vscode.window.showInformationMessage(
`Do you want to create ${preparePathFiles.length} file(s) in new "${newDirname}" folder?`,
...answersQuestion
);

if (resultQuestion !== answersQuestion[0]) {
return;
preparePathFiles.map(filePathTemplate => {
const filePath: string = renderVariableTemplate(filePathTemplate, [infoAboutNewDirectory]);
const newFilePath = path.join(uri.path, filePath);
const content = createFileContent(filePathTemplate, directories, [infoAboutNewDirectory]);

ensureDirectoryExistence(newFilePath);
fs.writeFile(newFilePath, content, {}, async () => {
const textDocument = await vscode.workspace.openTextDocument(newFilePath);
if (textDocument) {
vscode.window.showTextDocument(textDocument);
}
});
return filePath;
});


} else if(isFile(uri)) {
console.log(getInfoAboutPath(relative));
// fill files
}
} catch (error) {
const result = await vscode.window.showErrorMessage(
`Something go wrong :( ${error.toString()}`,
'Create issue od GitHub'
);

preparePathFiles.map(filePathTemplate => {
const filePath: string = renderVariableTemplate(filePathTemplate, [infoAboutNewDirectory]);
const newFilePath = path.join(uri.path, filePath);
const content = createFileContent(filePathTemplate, directories, [infoAboutNewDirectory]);

ensureDirectoryExistence(newFilePath);
fs.writeFile(newFilePath, content, {}, async () => {
const textDocument = await vscode.workspace.openTextDocument(newFilePath);
if (textDocument) {
vscode.window.showTextDocument(textDocument);
}
});
return filePath;
});
if (result === 'Create issue od GitHub') {
vscode.commands.executeCommand('vscode.open', vscode.Uri.parse('https://github.com/Bajdzis/vscode-awesome-tree/issues/new'));
}


} else if(isFile(uri)) {
console.log(getInfoAboutPath(relative));
// fill files
}
});

Expand Down

0 comments on commit 8d6bf32

Please sign in to comment.