From 4c91ead0aa36fafa2940484640c9d3167482b2e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerard=20Delma=CC=80s?= Date: Fri, 31 May 2019 03:26:43 +0200 Subject: [PATCH 1/8] make one file per class --- src/DtsContent.ts | 88 ++++++++++++++++++ src/{dtsCreator.ts => DtsCreator.ts} | 91 +------------------ ...ileSystemLoader.ts => FileSystemLoader.ts} | 41 +++++---- src/cli.ts | 2 +- src/index.ts | 2 +- test/dtsCreator.spec.js | 4 +- 6 files changed, 116 insertions(+), 112 deletions(-) create mode 100644 src/DtsContent.ts rename src/{dtsCreator.ts => DtsCreator.ts} (58%) rename src/{fileSystemLoader.ts => FileSystemLoader.ts} (97%) diff --git a/src/DtsContent.ts b/src/DtsContent.ts new file mode 100644 index 00000000..a2df6305 --- /dev/null +++ b/src/DtsContent.ts @@ -0,0 +1,88 @@ +import * as fs from "fs"; +import * as os from "os"; +import * as path from "path"; +import isThere from "is-there"; +import * as mkdirp from 'mkdirp'; + + +interface DtsContentOptions { + dropExtension: boolean; + rootDir: string; + searchDir: string; + outDir: string; + rInputPath: string; + rawTokenList: string[]; + resultList: string[]; + EOL: string; +} + +export class DtsContent { + private dropExtension: boolean; + private rootDir: string; + private searchDir: string; + private outDir: string; + private rInputPath: string; + private rawTokenList: string[]; + private resultList: string[]; + private EOL: string; + + constructor(options: DtsContentOptions) { + this.dropExtension = options.dropExtension; + this.rootDir = options.rootDir; + this.searchDir = options.searchDir; + this.outDir = options.outDir; + this.rInputPath = options.rInputPath; + this.rawTokenList = options.rawTokenList; + this.resultList = options.resultList; + this.EOL = options.EOL; + } + + public get contents(): string[] { + return this.resultList; + } + + public get formatted(): string { + if(!this.resultList || !this.resultList.length) return ''; + return [ + 'declare const styles: {', + ...this.resultList.map(line => ' ' + line), + '};', + 'export = styles;', + '' + ].join(os.EOL) + this.EOL; + } + + public get tokens(): string[] { + return this.rawTokenList; + } + + public get outputFilePath(): string { + const outputFileName = this.dropExtension ? removeExtension(this.rInputPath) : this.rInputPath; + return path.join(this.rootDir, this.outDir, outputFileName + '.d.ts'); + } + + public get inputFilePath(): string { + return path.join(this.rootDir, this.searchDir, this.rInputPath); + } + + public writeFile(): Promise { + var outPathDir = path.dirname(this.outputFilePath); + if(!isThere(outPathDir)) { + mkdirp.sync(outPathDir); + } + return new Promise((resolve, reject) => { + fs.writeFile(this.outputFilePath, this.formatted, 'utf8', (err) => { + if(err) { + reject(err); + }else{ + resolve(this); + } + }); + }); + } +} + +function removeExtension(filePath: string): string { + const ext = path.extname(filePath); + return filePath.replace(new RegExp(ext + '$'), ''); +} diff --git a/src/dtsCreator.ts b/src/DtsCreator.ts similarity index 58% rename from src/dtsCreator.ts rename to src/DtsCreator.ts index 9caa2c81..7c0cd032 100644 --- a/src/dtsCreator.ts +++ b/src/DtsCreator.ts @@ -1,95 +1,10 @@ import * as process from 'process'; -import * as fs from 'fs'; import * as path from'path'; - -import isThere from 'is-there'; -import * as mkdirp from 'mkdirp'; -import camelcase from "camelcase" - -import FileSystemLoader from './fileSystemLoader'; import * as os from 'os'; +import camelcase from "camelcase" +import FileSystemLoader from './FileSystemLoader'; +import {DtsContent} from "./DtsContent"; -function removeExtension(filePath: string): string { - const ext = path.extname(filePath); - return filePath.replace(new RegExp(ext + '$'), ''); -} - -interface DtsContentOptions { - dropExtension: boolean; - rootDir: string; - searchDir: string; - outDir: string; - rInputPath: string; - rawTokenList: string[]; - resultList: string[]; - EOL: string; -} - -class DtsContent { - private dropExtension: boolean; - private rootDir: string; - private searchDir: string; - private outDir: string; - private rInputPath: string; - private rawTokenList: string[]; - private resultList: string[]; - private EOL: string; - - constructor(options: DtsContentOptions) { - this.dropExtension = options.dropExtension; - this.rootDir = options.rootDir; - this.searchDir = options.searchDir; - this.outDir = options.outDir; - this.rInputPath = options.rInputPath; - this.rawTokenList = options.rawTokenList; - this.resultList = options.resultList; - this.EOL = options.EOL; - } - - public get contents(): string[] { - return this.resultList; - } - - public get formatted(): string { - if(!this.resultList || !this.resultList.length) return ''; - return [ - 'declare const styles: {', - ...this.resultList.map(line => ' ' + line), - '};', - 'export = styles;', - '' - ].join(os.EOL) + this.EOL; - } - - public get tokens(): string[] { - return this.rawTokenList; - } - - public get outputFilePath(): string { - const outputFileName = this.dropExtension ? removeExtension(this.rInputPath) : this.rInputPath; - return path.join(this.rootDir, this.outDir, outputFileName + '.d.ts'); - } - - public get inputFilePath(): string { - return path.join(this.rootDir, this.searchDir, this.rInputPath); - } - - public writeFile(): Promise { - var outPathDir = path.dirname(this.outputFilePath); - if(!isThere(outPathDir)) { - mkdirp.sync(outPathDir); - } - return new Promise((resolve, reject) => { - fs.writeFile(this.outputFilePath, this.formatted, 'utf8', (err) => { - if(err) { - reject(err); - }else{ - resolve(this); - } - }); - }); - } -} type CamelCaseOption = boolean | 'dashes' | undefined; diff --git a/src/fileSystemLoader.ts b/src/FileSystemLoader.ts similarity index 97% rename from src/fileSystemLoader.ts rename to src/FileSystemLoader.ts index c411ea30..f887e171 100644 --- a/src/fileSystemLoader.ts +++ b/src/FileSystemLoader.ts @@ -5,26 +5,6 @@ import * as fs from 'fs' import * as path from 'path' import { Plugin } from "postcss"; -// Sorts dependencies in the following way: -// AAA comes before AA and A -// AB comes after AA and before A -// All Bs come after all As -// This ensures that the files are always returned in the following order: -// - In the order they were required, except -// - After all their dependencies -const traceKeySorter = ( a: string, b: string ): number => { - if ( a.length < b.length ) { - return a < b.substring( 0, a.length ) ? -1 : 1 - } else if ( a.length > b.length ) { - return a.substring( 0, b.length ) <= b ? -1 : 1 - } else { - return a < b ? -1 : 1 - } -}; - -export type Dictionary = { - [key: string]: T | undefined; -}; export default class FileSystemLoader { private root: string; @@ -96,3 +76,24 @@ export default class FileSystemLoader { return this; } } + +// Sorts dependencies in the following way: +// AAA comes before AA and A +// AB comes after AA and before A +// All Bs come after all As +// This ensures that the files are always returned in the following order: +// - In the order they were required, except +// - After all their dependencies +function traceKeySorter( a: string, b: string ): number { + if ( a.length < b.length ) { + return a < b.substring( 0, a.length ) ? -1 : 1 + } else if ( a.length > b.length ) { + return a.substring( 0, b.length ) <= b ? -1 : 1 + } else { + return a < b ? -1 : 1 + } +}; + +type Dictionary = { + [key: string]: T | undefined; +}; diff --git a/src/cli.ts b/src/cli.ts index ebe75fea..ebcc81c4 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -5,7 +5,7 @@ import * as chokidar from 'chokidar'; import glob from 'glob'; import * as yargs from 'yargs'; import chalk from 'chalk'; -import {DtsCreator} from './dtsCreator'; +import {DtsCreator} from './DtsCreator'; let yarg = yargs.usage('Create .css.d.ts from CSS modules *.css files.\nUsage: $0 [options] ') .example('$0 src/styles', '') diff --git a/src/index.ts b/src/index.ts index f76ffac9..729f3986 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,2 @@ -import { DtsCreator } from './dtsCreator'; +import { DtsCreator } from './DtsCreator'; export = DtsCreator; diff --git a/test/dtsCreator.spec.js b/test/dtsCreator.spec.js index 51b812c9..9f3757b4 100644 --- a/test/dtsCreator.spec.js +++ b/test/dtsCreator.spec.js @@ -2,7 +2,7 @@ var path = require('path'); var assert = require('assert'); -var DtsCreator = require('../lib/dtsCreator').DtsCreator; +var DtsCreator = require('../lib/DtsCreator').DtsCreator; var os = require('os'); describe('DtsCreator', () => { @@ -190,4 +190,4 @@ export = styles; }); }); }); -}); \ No newline at end of file +}); From dc5b4e73c8e8688069f633315435691781bea423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerard=20Delma=CC=80s?= Date: Fri, 31 May 2019 03:33:25 +0200 Subject: [PATCH 2/8] remove dead code --- src/FileSystemLoader.ts | 35 ++++------------------------------- 1 file changed, 4 insertions(+), 31 deletions(-) diff --git a/src/FileSystemLoader.ts b/src/FileSystemLoader.ts index f887e171..76a690e5 100644 --- a/src/FileSystemLoader.ts +++ b/src/FileSystemLoader.ts @@ -6,6 +6,10 @@ import * as path from 'path' import { Plugin } from "postcss"; +type Dictionary = { + [key: string]: T | undefined; +}; + export default class FileSystemLoader { private root: string; private sources: Dictionary; @@ -65,35 +69,4 @@ export default class FileSystemLoader { } } ) } - - private get finalSource(): string { - return Object.keys( this.sources ).sort( traceKeySorter ).map( s => this.sources[s] ) - .join( "" ) - } - - private clear(): FileSystemLoader { - this.tokensByFile = {}; - return this; - } } - -// Sorts dependencies in the following way: -// AAA comes before AA and A -// AB comes after AA and before A -// All Bs come after all As -// This ensures that the files are always returned in the following order: -// - In the order they were required, except -// - After all their dependencies -function traceKeySorter( a: string, b: string ): number { - if ( a.length < b.length ) { - return a < b.substring( 0, a.length ) ? -1 : 1 - } else if ( a.length > b.length ) { - return a.substring( 0, b.length ) <= b ? -1 : 1 - } else { - return a < b ? -1 : 1 - } -}; - -type Dictionary = { - [key: string]: T | undefined; -}; From ae9d4201973be2115a9ff41756e917eb01647a42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerard=20Delma=CC=80s?= Date: Fri, 31 May 2019 03:50:47 +0200 Subject: [PATCH 3/8] use await on own promises --- src/DtsCreator.ts | 68 +++++++++++++++++++++++------------------------ src/cli.ts | 16 ++++++----- 2 files changed, 43 insertions(+), 41 deletions(-) diff --git a/src/DtsCreator.ts b/src/DtsCreator.ts index 7c0cd032..326ed7ed 100644 --- a/src/DtsCreator.ts +++ b/src/DtsCreator.ts @@ -41,45 +41,43 @@ export class DtsCreator { this.EOL = options.EOL || os.EOL; } - create(filePath: string, initialContents?: string, clearCache: boolean = false): Promise { - return new Promise((resolve, reject) => { - let rInputPath: string; - if(path.isAbsolute(filePath)) { - rInputPath = path.relative(this.inputDirectory, filePath); - }else{ - rInputPath = path.relative(this.inputDirectory, path.join(process.cwd(), filePath)); - } - if(clearCache) { - this.loader.tokensByFile = {}; - } - this.loader.fetch(filePath, "/", undefined, initialContents).then((res) => { - if(res) { - var tokens = res; - var keys = Object.keys(tokens); + public async create(filePath: string, initialContents?: string, clearCache: boolean = false): Promise { + let rInputPath: string; + if(path.isAbsolute(filePath)) { + rInputPath = path.relative(this.inputDirectory, filePath); + }else{ + rInputPath = path.relative(this.inputDirectory, path.join(process.cwd(), filePath)); + } + if(clearCache) { + this.loader.tokensByFile = {}; + } - var convertKey = this.getConvertKeyMethod(this.camelCase); + const res = await this.loader.fetch(filePath, "/", undefined, initialContents); + if(res) { + var tokens = res; + var keys = Object.keys(tokens); - var result = keys - .map(k => convertKey(k)) - .map(k => 'readonly "' + k + '": string;') + var convertKey = this.getConvertKeyMethod(this.camelCase); - var content = new DtsContent({ - dropExtension: this.dropExtension, - rootDir: this.rootDir, - searchDir: this.searchDir, - outDir: this.outDir, - rInputPath, - rawTokenList: keys, - resultList: result, - EOL: this.EOL - }); + var result = keys + .map(k => convertKey(k)) + .map(k => 'readonly "' + k + '": string;') - resolve(content); - }else{ - reject(res); - } - }).catch(err => reject(err)); - }); + var content = new DtsContent({ + dropExtension: this.dropExtension, + rootDir: this.rootDir, + searchDir: this.searchDir, + outDir: this.outDir, + rInputPath, + rawTokenList: keys, + resultList: result, + EOL: this.EOL + }); + + return content; + }else{ + throw res; + } } private getConvertKeyMethod(camelCaseOption: CamelCaseOption): (str: string) => string { diff --git a/src/cli.ts b/src/cli.ts index ebcc81c4..09b1d840 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -6,6 +6,7 @@ import glob from 'glob'; import * as yargs from 'yargs'; import chalk from 'chalk'; import {DtsCreator} from './DtsCreator'; +import {DtsContent} from "./DtsContent"; let yarg = yargs.usage('Create .css.d.ts from CSS modules *.css files.\nUsage: $0 [options] ') .example('$0 src/styles', '') @@ -24,15 +25,18 @@ let yarg = yargs.usage('Create .css.d.ts from CSS modules *.css files.\nUsage: $ let argv = yarg.argv; let creator: DtsCreator; -function writeFile(f: string) { - creator.create(f, undefined, !!argv.w) - .then(content => content.writeFile()) - .then(content => { +async function writeFile(f: string): Promise { + try { + const content: DtsContent = await creator.create(f, undefined, !!argv.w); + await content.writeFile(); + if (!argv.s) { console.log('Wrote ' + chalk.green(content.outputFilePath)); } - }) - .catch((reason: unknown) => console.error(chalk.red('[Error] ' + reason))); + } + catch (error) { + console.error(chalk.red('[Error] ' + error)); + } }; let main = () => { From e2a47697d29f3e8342774a705659103ae1ca2e14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerard=20Delma=CC=80s?= Date: Mon, 3 Jun 2019 02:05:58 +0200 Subject: [PATCH 4/8] use await in FileSystemLoader --- src/FileSystemLoader.ts | 87 +++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 42 deletions(-) diff --git a/src/FileSystemLoader.ts b/src/FileSystemLoader.ts index 76a690e5..2c6d2eda 100644 --- a/src/FileSystemLoader.ts +++ b/src/FileSystemLoader.ts @@ -3,6 +3,7 @@ import Core from 'css-modules-loader-core' import * as fs from 'fs' import * as path from 'path' +import * as util from 'util' import { Plugin } from "postcss"; @@ -10,6 +11,9 @@ type Dictionary = { [key: string]: T | undefined; }; +const readFile = util.promisify(fs.readFile); + + export default class FileSystemLoader { private root: string; private sources: Dictionary; @@ -18,55 +22,54 @@ export default class FileSystemLoader { public tokensByFile: Dictionary; constructor( root: string, plugins?: Array> ) { - this.root = root - this.sources = {} - this.importNr = 0 - this.core = new Core(plugins) + this.root = root; + this.sources = {}; + this.importNr = 0; + this.core = new Core(plugins); this.tokensByFile = {}; } - public fetch( _newPath: string, relativeTo: string, _trace?: string, initialContents?: string ): Promise { - let newPath = _newPath.replace( /^["']|["']$/g, "" ), - trace = _trace || String.fromCharCode( this.importNr++ ) - return new Promise( ( resolve, reject ) => { - let relativeDir = path.dirname( relativeTo ), - rootRelativePath = path.resolve( relativeDir, newPath ), - fileRelativePath = path.resolve( path.join( this.root, relativeDir ), newPath ) + public async fetch(_newPath: string, relativeTo: string, _trace?: string, initialContents?: string): Promise { + let newPath = _newPath.replace(/^["']|["']$/g, ""); + let trace = _trace || String.fromCharCode(this.importNr++); - // if the path is not relative or absolute, try to resolve it in node_modules - if (newPath[0] !== '.' && newPath[0] !== '/') { - try { - fileRelativePath = require.resolve(newPath); - } - catch (e) {} + let relativeDir = path.dirname(relativeTo); + let rootRelativePath = path.resolve(relativeDir, newPath); + let fileRelativePath = path.resolve(path.join(this.root, relativeDir), newPath); + + // if the path is not relative or absolute, try to resolve it in node_modules + if (newPath[0] !== '.' && newPath[0] !== '/') { + try { + fileRelativePath = require.resolve(newPath); + } + catch (e) {} + } + + let source: string; + + if (!initialContents) { + const tokens = this.tokensByFile[fileRelativePath] + if (tokens) { + return tokens; } - if(!initialContents) { - const tokens = this.tokensByFile[fileRelativePath] - if (tokens) { return resolve(tokens) } + try { + source = await readFile(fileRelativePath, "utf-8"); + } + catch (error) { + if (relativeTo && relativeTo !== '/') { + return {}; + } - fs.readFile( fileRelativePath, "utf-8", ( err, source ) => { - if ( err && relativeTo && relativeTo !== '/') { - resolve({}); - }else if ( err && (!relativeTo || relativeTo === '/')) { - reject(err); - }else{ - this.core.load( source, rootRelativePath, trace, this.fetch.bind( this ) ) - .then( ( { injectableSource, exportTokens } ) => { - this.sources[trace] = injectableSource - this.tokensByFile[fileRelativePath] = exportTokens - resolve( exportTokens ) - }, reject ) - } - } ) - }else{ - this.core.load( initialContents, rootRelativePath, trace, this.fetch.bind(this) ) - .then( ( { injectableSource, exportTokens } ) => { - this.sources[trace] = injectableSource - this.tokensByFile[fileRelativePath] = exportTokens - resolve( exportTokens ) - }, reject ) + throw error; } - } ) + } else { + source = initialContents; + } + + const { injectableSource, exportTokens } = await this.core.load(source, rootRelativePath, trace, this.fetch.bind(this)); + this.sources[trace] = injectableSource; + this.tokensByFile[fileRelativePath] = exportTokens; + return exportTokens; } } From 7e5ad2057b70d63dd8b0b8a9e13b7a1ea030a767 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerard=20Delma=CC=80s?= Date: Mon, 3 Jun 2019 02:23:13 +0200 Subject: [PATCH 5/8] use await with promisified node callback --- src/DtsContent.ts | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/DtsContent.ts b/src/DtsContent.ts index a2df6305..89a3d22b 100644 --- a/src/DtsContent.ts +++ b/src/DtsContent.ts @@ -3,6 +3,9 @@ import * as os from "os"; import * as path from "path"; import isThere from "is-there"; import * as mkdirp from 'mkdirp'; +import * as util from "util"; + +const writeFile = util.promisify(fs.writeFile); interface DtsContentOptions { @@ -65,20 +68,13 @@ export class DtsContent { return path.join(this.rootDir, this.searchDir, this.rInputPath); } - public writeFile(): Promise { + public async writeFile(): Promise { var outPathDir = path.dirname(this.outputFilePath); if(!isThere(outPathDir)) { mkdirp.sync(outPathDir); } - return new Promise((resolve, reject) => { - fs.writeFile(this.outputFilePath, this.formatted, 'utf8', (err) => { - if(err) { - reject(err); - }else{ - resolve(this); - } - }); - }); + + await writeFile(this.outputFilePath, this.formatted, 'utf8'); } } From 48fda1d730fc5a9174be8e25fcbd7bc45c49f9f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerard=20Delma=CC=80s?= Date: Tue, 4 Jun 2019 02:04:35 +0200 Subject: [PATCH 6/8] use let/const --- src/DtsContent.ts | 2 +- src/DtsCreator.ts | 10 +++++----- src/FileSystemLoader.ts | 8 ++++---- src/cli.ts | 13 +++++++------ 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/DtsContent.ts b/src/DtsContent.ts index 89a3d22b..ab3e0749 100644 --- a/src/DtsContent.ts +++ b/src/DtsContent.ts @@ -69,7 +69,7 @@ export class DtsContent { } public async writeFile(): Promise { - var outPathDir = path.dirname(this.outputFilePath); + const outPathDir = path.dirname(this.outputFilePath); if(!isThere(outPathDir)) { mkdirp.sync(outPathDir); } diff --git a/src/DtsCreator.ts b/src/DtsCreator.ts index 326ed7ed..642555d3 100644 --- a/src/DtsCreator.ts +++ b/src/DtsCreator.ts @@ -54,16 +54,16 @@ export class DtsCreator { const res = await this.loader.fetch(filePath, "/", undefined, initialContents); if(res) { - var tokens = res; - var keys = Object.keys(tokens); + const tokens = res; + const keys = Object.keys(tokens); - var convertKey = this.getConvertKeyMethod(this.camelCase); + const convertKey = this.getConvertKeyMethod(this.camelCase); - var result = keys + const result = keys .map(k => convertKey(k)) .map(k => 'readonly "' + k + '": string;') - var content = new DtsContent({ + const content = new DtsContent({ dropExtension: this.dropExtension, rootDir: this.rootDir, searchDir: this.searchDir, diff --git a/src/FileSystemLoader.ts b/src/FileSystemLoader.ts index 2c6d2eda..70f62e0f 100644 --- a/src/FileSystemLoader.ts +++ b/src/FileSystemLoader.ts @@ -30,11 +30,11 @@ export default class FileSystemLoader { } public async fetch(_newPath: string, relativeTo: string, _trace?: string, initialContents?: string): Promise { - let newPath = _newPath.replace(/^["']|["']$/g, ""); - let trace = _trace || String.fromCharCode(this.importNr++); + const newPath = _newPath.replace(/^["']|["']$/g, ""); + const trace = _trace || String.fromCharCode(this.importNr++); - let relativeDir = path.dirname(relativeTo); - let rootRelativePath = path.resolve(relativeDir, newPath); + const relativeDir = path.dirname(relativeTo); + const rootRelativePath = path.resolve(relativeDir, newPath); let fileRelativePath = path.resolve(path.join(this.root, relativeDir), newPath); // if the path is not relative or absolute, try to resolve it in node_modules diff --git a/src/cli.ts b/src/cli.ts index 09b1d840..f8eabc4d 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -8,7 +8,7 @@ import chalk from 'chalk'; import {DtsCreator} from './DtsCreator'; import {DtsContent} from "./DtsContent"; -let yarg = yargs.usage('Create .css.d.ts from CSS modules *.css files.\nUsage: $0 [options] ') +const yarg = yargs.usage('Create .css.d.ts from CSS modules *.css files.\nUsage: $0 [options] ') .example('$0 src/styles', '') .example('$0 src -o dist', '') .example('$0 -p styles/**/*.icss -w', '') @@ -22,7 +22,7 @@ let yarg = yargs.usage('Create .css.d.ts from CSS modules *.css files.\nUsage: $ .alias('s', 'silent').describe('s', 'Silent output. Do not show "files written" messages').boolean('s') .alias('h', 'help').help('h') .version(() => require('../package.json').version); -let argv = yarg.argv; +const argv = yarg.argv; let creator: DtsCreator; async function writeFile(f: string): Promise { @@ -39,8 +39,9 @@ async function writeFile(f: string): Promise { } }; -let main = () => { - let rootDir, searchDir; +function main() { + let rootDir: string; + let searchDir: string; if(argv.h) { yarg.showHelp(); return; @@ -54,7 +55,7 @@ let main = () => { yarg.showHelp(); return; } - let filesPattern = path.join(searchDir, argv.p || '**/*.css'); + const filesPattern = path.join(searchDir, argv.p || '**/*.css'); rootDir = process.cwd(); creator = new DtsCreator({ rootDir, @@ -76,7 +77,7 @@ let main = () => { } else { console.log('Watch ' + filesPattern + '...'); - var watcher = chokidar.watch([filesPattern.replace(/\\/g, "/")]); + const watcher = chokidar.watch([filesPattern.replace(/\\/g, "/")]); watcher.on('add', writeFile); watcher.on('change', writeFile); } From 7d4049f8d42b445c869cb9461b2345e0bbca5609 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerard=20Delma=CC=80s?= Date: Wed, 5 Jun 2019 00:48:36 +0200 Subject: [PATCH 7/8] support node 6 --- src/DtsContent.ts | 7 ++----- src/FileSystemLoader.ts | 7 ++----- src/fs.ts | 28 ++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 src/fs.ts diff --git a/src/DtsContent.ts b/src/DtsContent.ts index ab3e0749..07611ebf 100644 --- a/src/DtsContent.ts +++ b/src/DtsContent.ts @@ -1,11 +1,8 @@ -import * as fs from "fs"; import * as os from "os"; import * as path from "path"; import isThere from "is-there"; import * as mkdirp from 'mkdirp'; -import * as util from "util"; - -const writeFile = util.promisify(fs.writeFile); +import {writeFile} from "./fs"; interface DtsContentOptions { @@ -74,7 +71,7 @@ export class DtsContent { mkdirp.sync(outPathDir); } - await writeFile(this.outputFilePath, this.formatted, 'utf8'); + await writeFile(this.outputFilePath, this.formatted); } } diff --git a/src/FileSystemLoader.ts b/src/FileSystemLoader.ts index 70f62e0f..2e6a7d25 100644 --- a/src/FileSystemLoader.ts +++ b/src/FileSystemLoader.ts @@ -1,18 +1,15 @@ /* this file is forked from https://raw.githubusercontent.com/css-modules/css-modules-loader-core/master/src/file-system-loader.js */ import Core from 'css-modules-loader-core' -import * as fs from 'fs' import * as path from 'path' -import * as util from 'util' import { Plugin } from "postcss"; +import {readFile} from "./fs"; type Dictionary = { [key: string]: T | undefined; }; -const readFile = util.promisify(fs.readFile); - export default class FileSystemLoader { private root: string; @@ -54,7 +51,7 @@ export default class FileSystemLoader { } try { - source = await readFile(fileRelativePath, "utf-8"); + source = await readFile(fileRelativePath); } catch (error) { if (relativeTo && relativeTo !== '/') { diff --git a/src/fs.ts b/src/fs.ts new file mode 100644 index 00000000..f97ae4d3 --- /dev/null +++ b/src/fs.ts @@ -0,0 +1,28 @@ +import * as fs from 'fs'; + + +export function readFile(path: fs.PathLike): Promise { + return new Promise((resolve, reject) => { + fs.readFile(path, 'utf-8', (err, data) => { + if (err !== null) { + reject(err); + return; + } + + resolve(data); + }); + }); +} + +export function writeFile(path: fs.PathLike, data: string): Promise { + return new Promise((resolve, reject) => { + fs.writeFile(path, data, 'utf-8', (err) => { + if (err !== null) { + reject(err); + return; + } + + resolve(); + }); + }); +} From 36eeb417b3553498cba0be6995b8b9b95daabdb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerard=20Delma=CC=80s?= Date: Tue, 11 Jun 2019 23:36:23 +0200 Subject: [PATCH 8/8] remove node 6 support This reverts commit 7d4049f8d42b445c869cb9461b2345e0bbca5609 and disables running tests in travis on node 6 and specifies engine support in package.json --- .travis.yml | 1 - package.json | 3 +++ src/DtsContent.ts | 7 +++++-- src/FileSystemLoader.ts | 7 +++++-- src/fs.ts | 28 ---------------------------- 5 files changed, 13 insertions(+), 33 deletions(-) delete mode 100644 src/fs.ts diff --git a/.travis.yml b/.travis.yml index 8321f275..79388e00 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,6 @@ env: - GIT_AUTHOR_EMAIL=yosuke.kurami@gmail.com language: node_js node_js: -- 6 - 8 - 9 before_script: diff --git a/package.json b/package.json index 18b25d77..dfc934dd 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,9 @@ ], "author": "quramy", "license": "MIT", + "engines": { + "node": ">=8.0.0" + }, "dependencies": { "camelcase": "^5.3.1", "chalk": "^2.1.0", diff --git a/src/DtsContent.ts b/src/DtsContent.ts index 07611ebf..ab3e0749 100644 --- a/src/DtsContent.ts +++ b/src/DtsContent.ts @@ -1,8 +1,11 @@ +import * as fs from "fs"; import * as os from "os"; import * as path from "path"; import isThere from "is-there"; import * as mkdirp from 'mkdirp'; -import {writeFile} from "./fs"; +import * as util from "util"; + +const writeFile = util.promisify(fs.writeFile); interface DtsContentOptions { @@ -71,7 +74,7 @@ export class DtsContent { mkdirp.sync(outPathDir); } - await writeFile(this.outputFilePath, this.formatted); + await writeFile(this.outputFilePath, this.formatted, 'utf8'); } } diff --git a/src/FileSystemLoader.ts b/src/FileSystemLoader.ts index 2e6a7d25..70f62e0f 100644 --- a/src/FileSystemLoader.ts +++ b/src/FileSystemLoader.ts @@ -1,15 +1,18 @@ /* this file is forked from https://raw.githubusercontent.com/css-modules/css-modules-loader-core/master/src/file-system-loader.js */ import Core from 'css-modules-loader-core' +import * as fs from 'fs' import * as path from 'path' +import * as util from 'util' import { Plugin } from "postcss"; -import {readFile} from "./fs"; type Dictionary = { [key: string]: T | undefined; }; +const readFile = util.promisify(fs.readFile); + export default class FileSystemLoader { private root: string; @@ -51,7 +54,7 @@ export default class FileSystemLoader { } try { - source = await readFile(fileRelativePath); + source = await readFile(fileRelativePath, "utf-8"); } catch (error) { if (relativeTo && relativeTo !== '/') { diff --git a/src/fs.ts b/src/fs.ts deleted file mode 100644 index f97ae4d3..00000000 --- a/src/fs.ts +++ /dev/null @@ -1,28 +0,0 @@ -import * as fs from 'fs'; - - -export function readFile(path: fs.PathLike): Promise { - return new Promise((resolve, reject) => { - fs.readFile(path, 'utf-8', (err, data) => { - if (err !== null) { - reject(err); - return; - } - - resolve(data); - }); - }); -} - -export function writeFile(path: fs.PathLike, data: string): Promise { - return new Promise((resolve, reject) => { - fs.writeFile(path, data, 'utf-8', (err) => { - if (err !== null) { - reject(err); - return; - } - - resolve(); - }); - }); -}