|
| 1 | +import * as process from 'process'; |
| 2 | +import * as path from'path'; |
| 3 | +import * as os from 'os'; |
| 4 | +import camelcase from "camelcase" |
| 5 | +import FileSystemLoader from './FileSystemLoader'; |
| 6 | +import {DtsContent} from "./DtsContent"; |
| 7 | + |
| 8 | + |
| 9 | +type CamelCaseOption = boolean | 'dashes' | undefined; |
| 10 | + |
| 11 | +interface DtsCreatorOptions { |
| 12 | + rootDir?: string; |
| 13 | + searchDir?: string; |
| 14 | + outDir?: string; |
| 15 | + camelCase?: CamelCaseOption; |
| 16 | + dropExtension?: boolean; |
| 17 | + EOL?: string; |
| 18 | +} |
| 19 | + |
| 20 | +export class DtsCreator { |
| 21 | + private rootDir: string; |
| 22 | + private searchDir: string; |
| 23 | + private outDir: string; |
| 24 | + private loader: FileSystemLoader; |
| 25 | + private inputDirectory: string; |
| 26 | + private outputDirectory: string; |
| 27 | + private camelCase: boolean | 'dashes' | undefined; |
| 28 | + private dropExtension: boolean; |
| 29 | + private EOL: string; |
| 30 | + |
| 31 | + constructor(options?: DtsCreatorOptions) { |
| 32 | + if(!options) options = {}; |
| 33 | + this.rootDir = options.rootDir || process.cwd(); |
| 34 | + this.searchDir = options.searchDir || ''; |
| 35 | + this.outDir = options.outDir || this.searchDir; |
| 36 | + this.loader = new FileSystemLoader(this.rootDir); |
| 37 | + this.inputDirectory = path.join(this.rootDir, this.searchDir); |
| 38 | + this.outputDirectory = path.join(this.rootDir, this.outDir); |
| 39 | + this.camelCase = options.camelCase; |
| 40 | + this.dropExtension = !!options.dropExtension; |
| 41 | + this.EOL = options.EOL || os.EOL; |
| 42 | + } |
| 43 | + |
| 44 | + public async create(filePath: string, initialContents?: string, clearCache: boolean = false): Promise<DtsContent> { |
| 45 | + let rInputPath: string; |
| 46 | + if(path.isAbsolute(filePath)) { |
| 47 | + rInputPath = path.relative(this.inputDirectory, filePath); |
| 48 | + }else{ |
| 49 | + rInputPath = path.relative(this.inputDirectory, path.join(process.cwd(), filePath)); |
| 50 | + } |
| 51 | + if(clearCache) { |
| 52 | + this.loader.tokensByFile = {}; |
| 53 | + } |
| 54 | + |
| 55 | + const res = await this.loader.fetch(filePath, "/", undefined, initialContents); |
| 56 | + if(res) { |
| 57 | + const tokens = res; |
| 58 | + const keys = Object.keys(tokens); |
| 59 | + |
| 60 | + const convertKey = this.getConvertKeyMethod(this.camelCase); |
| 61 | + |
| 62 | + const result = keys |
| 63 | + .map(k => convertKey(k)) |
| 64 | + .map(k => 'readonly "' + k + '": string;') |
| 65 | + |
| 66 | + const content = new DtsContent({ |
| 67 | + dropExtension: this.dropExtension, |
| 68 | + rootDir: this.rootDir, |
| 69 | + searchDir: this.searchDir, |
| 70 | + outDir: this.outDir, |
| 71 | + rInputPath, |
| 72 | + rawTokenList: keys, |
| 73 | + resultList: result, |
| 74 | + EOL: this.EOL |
| 75 | + }); |
| 76 | + |
| 77 | + return content; |
| 78 | + }else{ |
| 79 | + throw res; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private getConvertKeyMethod(camelCaseOption: CamelCaseOption): (str: string) => string { |
| 84 | + switch (camelCaseOption) { |
| 85 | + case true: |
| 86 | + return camelcase; |
| 87 | + case 'dashes': |
| 88 | + return this.dashesCamelCase; |
| 89 | + default: |
| 90 | + return (key) => key; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Replaces only the dashes and leaves the rest as-is. |
| 96 | + * |
| 97 | + * Mirrors the behaviour of the css-loader: |
| 98 | + * https://github.com/webpack-contrib/css-loader/blob/1fee60147b9dba9480c9385e0f4e581928ab9af9/lib/compile-exports.js#L3-L7 |
| 99 | + */ |
| 100 | + private dashesCamelCase(str: string): string { |
| 101 | + return str.replace(/-+(\w)/g, function(match, firstLetter) { |
| 102 | + return firstLetter.toUpperCase(); |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | +} |
0 commit comments