-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
171 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
out | ||
node_modules | ||
node_modules | ||
.vscode-test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './add-current-file-exportation-to-index'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters