Skip to content

Commit

Permalink
Scaffold create-index extension
Browse files Browse the repository at this point in the history
  • Loading branch information
tsugitta committed Sep 4, 2017
1 parent da18b9b commit 8abf014
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
out
node_modules
node_modules
.vscode-test
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@
"test": "node ./node_modules/vscode/bin/test"
},
"devDependencies": {
"@types/mkdirp": "^0.5.1",
"@types/mocha": "^2.2.32",
"@types/node": "^8.0.26",
"mkdirp": "^0.5.1",
"mocha": "^3.5.0",
"rimraf": "^2.6.1",
"tslint": "^5.7.0",
"typescript": "^2.0.3",
"vscode": "^1.0.0"
Expand Down
71 changes: 71 additions & 0 deletions src/commands/add-current-file-exportation-to-index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import * as path from 'path';
import * as vscode from 'vscode';
import * as editor from '../utils/editor-helper';
import { ApplicationError } from '../utils/errors';
import * as fileManager from '../utils/file-manager';

const INDEX_FILE_NAME = 'index.ts';

const getFilePath = (): string => {
if (!editor.fileIsOpened()) {
throw new ApplicationError('No file is opened.');
}

if (!editor.fileIsSaved()) {
throw new ApplicationError('The file is not saved yet.');
}

return editor.getCurrentFilePath();
};

const getIndexPath = (filePath: string): string => {
const dirPath = path.dirname(filePath);
return path.join(dirPath, INDEX_FILE_NAME);
};

const getExportationLine = (filePath: string): string => {
const fileName = path.basename(filePath);
const fileNameWithoutExtension = fileName.split('.')[0];
return `export * from './${fileNameWithoutExtension}';`;
};

const writeLineAndSort = (filePath: string, line: string): void => {
const lines = fileManager.getLines(filePath).filter(l => l !== '');

if (!lines.includes(line)) {
lines.push(line);
}

lines.sort();
const written = `${lines.join('\n')}\n`;

fileManager.writeFile(filePath, written);
};

export const addCurrentFileExportationToIndex = () => {
try {
const filePath = getFilePath();
const indexFilePath = getIndexPath(filePath);

if (filePath === indexFilePath) {
throw new ApplicationError('The file is index.ts itself.');
}

if (!filePath.match(/\.ts$/)) {
throw new ApplicationError('The file is not TypeScript.');
}

fileManager.createFileIfNotExists(indexFilePath);

const exportationLine = getExportationLine(filePath);
writeLineAndSort(indexFilePath, exportationLine);
} catch (err) {
if (err instanceof ApplicationError) {
vscode.window.showErrorMessage(err.message);
return;
}

throw err;
}

};
1 change: 1 addition & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './add-current-file-exportation-to-index';
10 changes: 5 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as vscode from 'vscode';
import * as commands from './commands';

export function activate(context: vscode.ExtensionContext) {
const disposable = vscode.commands.registerCommand('extension.addCurrentFileExportationToIndex', () => {
commands.addCurrentFileExportationToIndex();
});

const disposable = vscode.commands.registerCommand('extension.addCurrentFileExportationToIndex', () => {
vscode.window.showInformationMessage('foo');
});

context.subscriptions.push(disposable);
context.subscriptions.push(disposable);
}

export function deactivate() {
Expand Down
22 changes: 22 additions & 0 deletions src/utils/editor-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as vscode from 'vscode';

export const fileIsOpened = (): boolean => {
return !!vscode.window.activeTextEditor;
};

export const fileIsSaved = (): boolean => {
if (!fileIsOpened) {
return false;
}

const document = vscode.window.activeTextEditor.document;
return !document.isUntitled;
};

export const getCurrentFilePath = (): string | null => {
if (!(fileIsOpened && fileIsSaved)) {
return null;
}

return vscode.window.activeTextEditor.document.fileName;
};
12 changes: 12 additions & 0 deletions src/utils/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export class ApplicationError implements Error {
public name = 'ApplicationError';

constructor(
public message: string,
) {
}

public toString() {
return `${this.name}: ${this.message}`;
}
}
40 changes: 40 additions & 0 deletions src/utils/file-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as fs from 'fs';
import * as mkdirp from 'mkdirp';
import * as path from 'path';
import * as vscode from 'vscode';
import { ApplicationError } from './errors';

export const fileExists = (filePath: string): boolean => {
return fs.existsSync(filePath);
};

export const createFile = (filePath: string): void => {
if (fileExists(filePath)) {
throw new FileAlreadyExistsError(`${filePath} already exists`);
}

fs.appendFile(filePath, '', err => {
if (err) {
throw err;
}
});
};

export const createFileIfNotExists = (filePath: string): void => {
if (fileExists(filePath)) {
return;
}

createFile(filePath);
};

export const getLines = (filePath: string): string[] => {
return fs.readFileSync(filePath, 'utf-8').split('\n');
};

export const writeFile = (filePath: string, data: string): void => {
fs.writeFileSync(filePath, data);
};

export class FileAlreadyExistsError extends ApplicationError {
}
3 changes: 3 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './errors';
export * from './file-manager';
export * from './editor-helper';
7 changes: 4 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"target": "es2015",
"outDir": "out",
"lib": [
"es6"
"es6",
"es7"
],
"sourceMap": true,
"rootDir": "."
Expand All @@ -13,4 +14,4 @@
"node_modules",
".vscode-test"
]
}
}
10 changes: 8 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
# yarn lockfile v1


"@types/mkdirp@^0.5.1":
version "0.5.1"
resolved "https://registry.yarnpkg.com/@types/mkdirp/-/mkdirp-0.5.1.tgz#ea887cd024f691c1ca67cce20b7606b053e43b0f"
dependencies:
"@types/node" "*"

"@types/mocha@^2.2.32":
version "2.2.42"
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-2.2.42.tgz#ab769f51d37646b6fe8d4a086a98c285b1fab3f5"

"@types/node@^8.0.26":
"@types/node@*", "@types/node@^8.0.26":
version "8.0.26"
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.26.tgz#4d58be925306fd22b1141085535a0268b8beb189"

Expand Down Expand Up @@ -1281,7 +1287,7 @@ resolve@^1.3.2:
dependencies:
path-parse "^1.0.5"

rimraf@2:
rimraf@2, rimraf@^2.6.1:
version "2.6.1"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.1.tgz#c2338ec643df7a1b7fe5c54fa86f57428a55f33d"
dependencies:
Expand Down

0 comments on commit 8abf014

Please sign in to comment.