Skip to content

Commit 8abf014

Browse files
committed
Scaffold create-index extension
1 parent da18b9b commit 8abf014

File tree

11 files changed

+171
-11
lines changed

11 files changed

+171
-11
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
out
2-
node_modules
2+
node_modules
3+
.vscode-test

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@
2929
"test": "node ./node_modules/vscode/bin/test"
3030
},
3131
"devDependencies": {
32+
"@types/mkdirp": "^0.5.1",
3233
"@types/mocha": "^2.2.32",
3334
"@types/node": "^8.0.26",
35+
"mkdirp": "^0.5.1",
3436
"mocha": "^3.5.0",
37+
"rimraf": "^2.6.1",
3538
"tslint": "^5.7.0",
3639
"typescript": "^2.0.3",
3740
"vscode": "^1.0.0"
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import * as path from 'path';
2+
import * as vscode from 'vscode';
3+
import * as editor from '../utils/editor-helper';
4+
import { ApplicationError } from '../utils/errors';
5+
import * as fileManager from '../utils/file-manager';
6+
7+
const INDEX_FILE_NAME = 'index.ts';
8+
9+
const getFilePath = (): string => {
10+
if (!editor.fileIsOpened()) {
11+
throw new ApplicationError('No file is opened.');
12+
}
13+
14+
if (!editor.fileIsSaved()) {
15+
throw new ApplicationError('The file is not saved yet.');
16+
}
17+
18+
return editor.getCurrentFilePath();
19+
};
20+
21+
const getIndexPath = (filePath: string): string => {
22+
const dirPath = path.dirname(filePath);
23+
return path.join(dirPath, INDEX_FILE_NAME);
24+
};
25+
26+
const getExportationLine = (filePath: string): string => {
27+
const fileName = path.basename(filePath);
28+
const fileNameWithoutExtension = fileName.split('.')[0];
29+
return `export * from './${fileNameWithoutExtension}';`;
30+
};
31+
32+
const writeLineAndSort = (filePath: string, line: string): void => {
33+
const lines = fileManager.getLines(filePath).filter(l => l !== '');
34+
35+
if (!lines.includes(line)) {
36+
lines.push(line);
37+
}
38+
39+
lines.sort();
40+
const written = `${lines.join('\n')}\n`;
41+
42+
fileManager.writeFile(filePath, written);
43+
};
44+
45+
export const addCurrentFileExportationToIndex = () => {
46+
try {
47+
const filePath = getFilePath();
48+
const indexFilePath = getIndexPath(filePath);
49+
50+
if (filePath === indexFilePath) {
51+
throw new ApplicationError('The file is index.ts itself.');
52+
}
53+
54+
if (!filePath.match(/\.ts$/)) {
55+
throw new ApplicationError('The file is not TypeScript.');
56+
}
57+
58+
fileManager.createFileIfNotExists(indexFilePath);
59+
60+
const exportationLine = getExportationLine(filePath);
61+
writeLineAndSort(indexFilePath, exportationLine);
62+
} catch (err) {
63+
if (err instanceof ApplicationError) {
64+
vscode.window.showErrorMessage(err.message);
65+
return;
66+
}
67+
68+
throw err;
69+
}
70+
71+
};

src/commands/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './add-current-file-exportation-to-index';

src/extension.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import * as vscode from 'vscode';
2+
import * as commands from './commands';
23

34
export function activate(context: vscode.ExtensionContext) {
5+
const disposable = vscode.commands.registerCommand('extension.addCurrentFileExportationToIndex', () => {
6+
commands.addCurrentFileExportationToIndex();
7+
});
48

5-
const disposable = vscode.commands.registerCommand('extension.addCurrentFileExportationToIndex', () => {
6-
vscode.window.showInformationMessage('foo');
7-
});
8-
9-
context.subscriptions.push(disposable);
9+
context.subscriptions.push(disposable);
1010
}
1111

1212
export function deactivate() {

src/utils/editor-helper.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as vscode from 'vscode';
2+
3+
export const fileIsOpened = (): boolean => {
4+
return !!vscode.window.activeTextEditor;
5+
};
6+
7+
export const fileIsSaved = (): boolean => {
8+
if (!fileIsOpened) {
9+
return false;
10+
}
11+
12+
const document = vscode.window.activeTextEditor.document;
13+
return !document.isUntitled;
14+
};
15+
16+
export const getCurrentFilePath = (): string | null => {
17+
if (!(fileIsOpened && fileIsSaved)) {
18+
return null;
19+
}
20+
21+
return vscode.window.activeTextEditor.document.fileName;
22+
};

src/utils/errors.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export class ApplicationError implements Error {
2+
public name = 'ApplicationError';
3+
4+
constructor(
5+
public message: string,
6+
) {
7+
}
8+
9+
public toString() {
10+
return `${this.name}: ${this.message}`;
11+
}
12+
}

src/utils/file-manager.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as fs from 'fs';
2+
import * as mkdirp from 'mkdirp';
3+
import * as path from 'path';
4+
import * as vscode from 'vscode';
5+
import { ApplicationError } from './errors';
6+
7+
export const fileExists = (filePath: string): boolean => {
8+
return fs.existsSync(filePath);
9+
};
10+
11+
export const createFile = (filePath: string): void => {
12+
if (fileExists(filePath)) {
13+
throw new FileAlreadyExistsError(`${filePath} already exists`);
14+
}
15+
16+
fs.appendFile(filePath, '', err => {
17+
if (err) {
18+
throw err;
19+
}
20+
});
21+
};
22+
23+
export const createFileIfNotExists = (filePath: string): void => {
24+
if (fileExists(filePath)) {
25+
return;
26+
}
27+
28+
createFile(filePath);
29+
};
30+
31+
export const getLines = (filePath: string): string[] => {
32+
return fs.readFileSync(filePath, 'utf-8').split('\n');
33+
};
34+
35+
export const writeFile = (filePath: string, data: string): void => {
36+
fs.writeFileSync(filePath, data);
37+
};
38+
39+
export class FileAlreadyExistsError extends ApplicationError {
40+
}

src/utils/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './errors';
2+
export * from './file-manager';
3+
export * from './editor-helper';

tsconfig.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"compilerOptions": {
33
"module": "commonjs",
4-
"target": "es6",
4+
"target": "es2015",
55
"outDir": "out",
66
"lib": [
7-
"es6"
7+
"es6",
8+
"es7"
89
],
910
"sourceMap": true,
1011
"rootDir": "."
@@ -13,4 +14,4 @@
1314
"node_modules",
1415
".vscode-test"
1516
]
16-
}
17+
}

0 commit comments

Comments
 (0)