Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for different extension #88

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const yarg = yargs.usage('Create .css.d.ts from CSS modules *.css files.\nUsage:
.alias('d', 'dropExtension').describe('d', 'Drop the input files extension').boolean('d')
.alias('s', 'silent').describe('s', 'Silent output. Do not show "files written" messages').boolean('s')
.alias('h', 'help').help('h')
.alias('e', 'extension').describe('s', 'Use this extension, instead of \'.d.ts\'')
.version(require('../package.json').version);


Expand Down Expand Up @@ -46,6 +47,7 @@ async function main(): Promise<void> {
watch: argv.w,
camelCase: argv.c,
dropExtension: argv.d,
silent: argv.s
silent: argv.s,
extension:argv.e
});
};
13 changes: 8 additions & 5 deletions src/dts-content.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
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 os from "os";
import * as path from "path";
import * as util from "util";

const writeFile = util.promisify(fs.writeFile);


interface DtsContentOptions {
dropExtension: boolean;
extension: string;
rootDir: string;
searchDir: string;
outDir: string;
Expand All @@ -28,6 +29,7 @@ export class DtsContent {
private rawTokenList: string[];
private resultList: string[];
private EOL: string;
private extension: string;

constructor(options: DtsContentOptions) {
this.dropExtension = options.dropExtension;
Expand All @@ -38,14 +40,15 @@ export class DtsContent {
this.rawTokenList = options.rawTokenList;
this.resultList = options.resultList;
this.EOL = options.EOL;
this.extension = options.extension;
}

public get contents(): string[] {
return this.resultList;
}

public get formatted(): string {
if(!this.resultList || !this.resultList.length) return '';
if (!this.resultList || !this.resultList.length) return '';
return [
'declare const styles: {',
...this.resultList.map(line => ' ' + line),
Expand All @@ -61,7 +64,7 @@ export class DtsContent {

public get outputFilePath(): string {
const outputFileName = this.dropExtension ? removeExtension(this.rInputPath) : this.rInputPath;
return path.join(this.rootDir, this.outDir, outputFileName + '.d.ts');
return path.join(this.rootDir, this.outDir, outputFileName + this.extension);
}

public get inputFilePath(): string {
Expand All @@ -72,7 +75,7 @@ export class DtsContent {
const finalOutput = postprocessor(this.formatted);

const outPathDir = path.dirname(this.outputFilePath);
if(!isThere(outPathDir)) {
if (!isThere(outPathDir)) {
mkdirp.sync(outPathDir);
}

Expand Down
4 changes: 4 additions & 0 deletions src/dts-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface DtsCreatorOptions {
dropExtension?: boolean;
EOL?: string;
loaderPlugins?: Plugin<any>[];
extension?:string;
}

export class DtsCreator {
Expand All @@ -29,6 +30,7 @@ export class DtsCreator {
private camelCase: boolean | 'dashes' | undefined;
private dropExtension: boolean;
private EOL: string;
private extension:string;

constructor(options?: DtsCreatorOptions) {
if(!options) options = {};
Expand All @@ -40,6 +42,7 @@ export class DtsCreator {
this.outputDirectory = path.join(this.rootDir, this.outDir);
this.camelCase = options.camelCase;
this.dropExtension = !!options.dropExtension;
this.extension = options.extension || '.d.ts';
this.EOL = options.EOL || os.EOL;
}

Expand Down Expand Up @@ -67,6 +70,7 @@ export class DtsCreator {

const content = new DtsContent({
dropExtension: this.dropExtension,
extension:this.extension,
rootDir: this.rootDir,
searchDir: this.searchDir,
outDir: this.outDir,
Expand Down
4 changes: 3 additions & 1 deletion src/file-system-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export default class FileSystemLoader {
this.root = root;
this.sources = {};
this.importNr = 0;
this.core = new Core(plugins);

//@ts-ignore
this.core = new Core(plugins );
this.tokensByFile = {};
}

Expand Down
16 changes: 9 additions & 7 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as path from "path";
import * as util from "util";
import chalk from "chalk";
import * as chokidar from "chokidar";
import _glob from 'glob';
import {DtsCreator} from "./dts-creator";
import * as path from "path";
import * as util from "util";
import {DtsContent} from "./dts-content";
import {DtsCreator} from "./dts-creator";

const glob = util.promisify(_glob);

Expand All @@ -16,6 +16,7 @@ interface RunOptions {
camelCase?: boolean;
dropExtension?: boolean;
silent?: boolean;
extension?: string;
}

export async function run(searchDir: string, options: RunOptions = {}): Promise<void> {
Expand All @@ -27,6 +28,7 @@ export async function run(searchDir: string, options: RunOptions = {}): Promise<
outDir: options.outDir,
camelCase: options.camelCase,
dropExtension: options.dropExtension,
extension: options.extension,
});

const writeFile = async (f: string): Promise<void> => {
Expand All @@ -37,13 +39,12 @@ export async function run(searchDir: string, options: RunOptions = {}): Promise<
if (!options.silent) {
console.log('Wrote ' + chalk.green(content.outputFilePath));
}
}
catch (error) {
} catch (error) {
console.error(chalk.red('[Error] ' + error));
}
};

if(!options.watch) {
if (!options.watch) {
const files = await glob(filesPattern);
await Promise.all(files.map(writeFile));
} else {
Expand All @@ -57,5 +58,6 @@ export async function run(searchDir: string, options: RunOptions = {}): Promise<
}

async function waitForever(): Promise<void> {
return new Promise<void>(() => {});
return new Promise<void>(() => {
});
}
13 changes: 12 additions & 1 deletion test/dts-creator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import * as path from 'path';

import * as assert from 'assert';
import * as os from 'os';
import { DtsCreator } from '../src/dts-creator';

describe('DtsCreator', () => {
Expand Down Expand Up @@ -96,6 +95,18 @@ describe('DtsContent', () => {
done();
});
});
it('can change the extension when asked', done => {
new DtsCreator({extension: '.what.ts'}).create(path.normalize('test/testStyle.css')).then(content => {
assert.equal(path.relative(process.cwd(), content.outputFilePath), path.normalize("test/testStyle.css.what.ts"));
done();
});
});
it('can change the drop and change extension when asked', done => {
new DtsCreator({dropExtension:true, extension: '.what.ts'}).create(path.normalize('test/testStyle.css')).then(content => {
assert.equal(path.relative(process.cwd(), content.outputFilePath), path.normalize("test/testStyle.what.ts"));
done();
});
});
});

describe('#formatted', () => {
Expand Down
Loading