Skip to content

Commit

Permalink
feat: fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneslumpe committed Nov 13, 2018
1 parent 076ba50 commit a8505c8
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 21 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
/.vscode
/workbench
/lib
/es
/es
/generated
8 changes: 7 additions & 1 deletion generate.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import * as fs from 'fs';
import { map } from 'lodash/fp';
import * as path from 'path';
import prettier from 'prettier';
import * as util from 'util';

import { generateAllTypes } from './src/generateAllTypes';
import { generateIndex } from './src/generateIndex';

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

const LIB_PATH = path.join(__dirname, './lib');
const LIB_PATH = path.join(__dirname, './generated');

async function format(filepath: string) {
const contents = await readFile(filepath);
Expand All @@ -33,6 +35,10 @@ async function generate() {
await Promise.all(
formatted.map(({ contents, filepath }) => writeFile(filepath, contents)),
);

await generateIndex(
map(filename => `./${filename.replace('.ts', '')}`, filenames),
);
}

generate();
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = {
collectCoverage: true,
globals: {
'ts-jest': {
tsConfigFile: 'tsconfig.cjs.json',
tsConfigFile: 'tsconfig.cjs.test.json',
},
},
};
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
"name": "@johanneslumpe/css-types",
"version": "0.1.0",
"description": "TypeScript CSS types and value helpers generated from MDN data",
"main": "lib/index.ts",
"main": "lib/index.js",
"module": "es/index.js",
"types": "lib/index.d.ts",
"scripts": {
"test": "tsc -p ./tsconfig.cjs.json --noEmit && jest",
"test": "tsc -p ./tsconfig.cjs.test.json --noEmit && jest",
"lint": "tslint --project tsconfig.json",
"format": "prettier --write \"src/**/*.ts\"",
"test:watch": "jest --watch",
"build": "rimraf ./lib && npx ts-node generate",
"build": "rimraf ./generated && npx ts-node generate && tsc -p ./tsconfig.cjs.generated.json && tsc -p ./tsconfig.esm.generated.json",
"prepare": "npm run build && npm run docs",
"prepublishOnly": "npm test && npm run lint",
"preversion": "npm run lint",
"version": "npm run format && git add -A src",
"postversion": "git push && git push --tags",
"docs": "typedoc ./lib --tsconfig ./tsconfig.cjs.json --hideGenerator --readme none --theme markdown --out docs"
"docs": "typedoc ./generated --tsconfig ./tsconfig.cjs.json --hideGenerator --readme none --theme markdown --out docs"
},
"files": [
"/lib",
Expand Down
3 changes: 2 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const TYPES_BUILD_DIR = '../lib/';
export const TYPES_BUILD_DIR = '../generated/';
export const LENGTH_GENERIC_ARGUMENT = 'TLength';
export const LENGTH_TYPE_NAME = 'Length';
export const CSS_TYPES_FILE = 'types.ts';
export const UNIT_TYPES_FILE = 'unitTypes.ts';
export const UNIT_UTILS_FILE = 'unitUtils.ts';
4 changes: 2 additions & 2 deletions src/generateAllTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import rimraf from 'rimraf';
import ts from 'typescript';
import * as util from 'util';

import { LENGTH_TYPE_NAME, TYPES_BUILD_DIR } from './constants';
import { CSS_TYPES_FILE, LENGTH_TYPE_NAME, TYPES_BUILD_DIR } from './constants';
import { customSyntaxes } from './customSyntaxes';
import {
generateBaseDataTypes,
Expand Down Expand Up @@ -94,7 +94,7 @@ export async function generateAllTypes() {

const typesOutputSource = ts.updateSourceFileNode(
ts.createSourceFile(
'types.ts',
CSS_TYPES_FILE,
'',
ts.ScriptTarget.ESNext,
false,
Expand Down
42 changes: 42 additions & 0 deletions src/generateIndex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as fs from 'fs';
import { map } from 'lodash/fp';
import * as path from 'path';
import ts from 'typescript';
import * as util from 'util';
import { TYPES_BUILD_DIR } from './constants';

export function generateIndex(filenames: string[]) {
const printer = ts.createPrinter({
newLine: ts.NewLineKind.LineFeed,
});

const indexSource = ts.createSourceFile(
'index.ts',
'',
ts.ScriptTarget.ESNext,
false,
ts.ScriptKind.TS,
);
const exportDeclarations = map(
filename =>
ts.createExportDeclaration(
[],
[],
undefined,
ts.createStringLiteral(filename),
),
filenames,
);

// const export
const updatedSource = ts.updateSourceFileNode(
indexSource,
exportDeclarations,
);

const writeFile = util.promisify(fs.writeFile);
return writeFile(
path.join(__dirname, TYPES_BUILD_DIR, indexSource.fileName),
printer.printFile(updatedSource),
);
}
19 changes: 10 additions & 9 deletions src/generateUnitTypeSourceFiles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { flatten, map } from 'lodash/fp';
import { flatten, map, sortBy } from 'lodash/fp';
import { CSSUnitGroups } from 'mdn-data';
import ts from 'typescript';

Expand All @@ -12,14 +12,15 @@ import { createUnitFunctionDeclaration } from './utils/createUnitFunctionDeclara
import { generateTypeName } from './utils/generateTypeName';
import { getUnits, lengthValueTags } from './utils/getUnits';

const getUnitsWithAdditionalTypes = (tagFilterArray?: CSSUnitGroups[]) => [
...getUnits(tagFilterArray),
{
brandKey: 'percentage',
name: 'Percentage',
unit: '%',
},
];
const getUnitsWithAdditionalTypes = (tagFilterArray?: CSSUnitGroups[]) =>
sortBy(x => x.name.toLowerCase(), [
...getUnits(tagFilterArray),
{
brandKey: 'percentage',
name: 'Percentage',
unit: '%',
},
]);

export function generateUnitInterfaces(
withAdditionalTypes: boolean,
Expand Down
7 changes: 7 additions & 0 deletions tsconfig.cjs.generated.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.compile.generated.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "lib"
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion tsconfig.compile.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
},
"extends": "./tsconfig.json",
"include": [
"src/**/*",
"generated/**/*",
],
"exclude": [
"**/*/__tests__/**/*"
Expand Down
12 changes: 12 additions & 0 deletions tsconfig.compile.generated.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "es5",
},
"extends": "./tsconfig.json",
"include": [
"generated/**/*",
],
"exclude": [
"**/*/__tests__/**/*"
]
}
2 changes: 1 addition & 1 deletion tsconfig.esm.json → tsconfig.esm.generated.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "./tsconfig.compile.base.json",
"extends": "./tsconfig.compile.generated.json",
"compilerOptions": {
"module": "esnext",
"outDir": "es"
Expand Down

0 comments on commit a8505c8

Please sign in to comment.