diff --git a/.eslintignore b/.eslintignore index c9b918a0..ce1b3219 100644 --- a/.eslintignore +++ b/.eslintignore @@ -7,3 +7,8 @@ packages/bemjson-to-jsx-demo #Remove after AVA done packages/config/test/*.test.js + +# Build + +es +lib diff --git a/.gitignore b/.gitignore index 3df0ccd3..ad34849f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,9 @@ coverage node_modules npm-debug.log lerna-debug.log + +# Build + +es +lib +typings diff --git a/package.json b/package.json index bbcd4f45..c6014e01 100644 --- a/package.json +++ b/package.json @@ -28,9 +28,10 @@ "nyc": "^11.0.3", "proxyquire": "^1.8.0", "sinon": "^2.3.6", + "ts-node": "^7.0.1", "tslint": "^5.0.0", "tslint-config-typings": "^0.3.1", - "typescript": "^2.4.1" + "typescript": "^3.0.1" }, "scripts": { "lint": "npm run lint:js && npm run lint:dts", diff --git a/packages/naming.presets/index.d.ts b/packages/naming.presets/index.d.ts deleted file mode 100644 index 014e73a1..00000000 --- a/packages/naming.presets/index.d.ts +++ /dev/null @@ -1,23 +0,0 @@ -declare module '@bem/sdk.naming.presets' { - interface INamingConventionDelims { - elem: string; - mod: string | { - name: string; - val: string; - }; - } - - export interface INamingConvention { - delims: INamingConventionDelims; - fs: { - pattern: string; - scheme: string; - delims: INamingConventionDelims; - }; - wordPattern: string; - } - - // TODO: Add export for two-dashes (https://github.com/bem/bem-sdk/issues/315) - export const react: INamingConvention; - export const origin: INamingConvention; -} diff --git a/packages/naming.presets/index.js b/packages/naming.presets/index.js deleted file mode 100644 index 4a06d14d..00000000 --- a/packages/naming.presets/index.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict'; - -exports.default = require('./legacy'); - -exports.legacy = require('./legacy'); -exports.origin = require('./origin'); -exports.react = require('./react'); -exports['origin-react'] = require('./origin-react'); -exports['two-dashes'] = require('./two-dashes'); diff --git a/packages/naming.presets/legacy.js b/packages/naming.presets/legacy.js deleted file mode 100644 index d65119d0..00000000 --- a/packages/naming.presets/legacy.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict'; - -const origin = require('./origin'); - -module.exports = Object.assign({}, origin, { - fs: Object.assign({}, origin.fs, { - pattern: '${entity}${layer?@${layer}}.${tech}', - }) -}); diff --git a/packages/naming.presets/origin-react.js b/packages/naming.presets/origin-react.js deleted file mode 100644 index 761ff2cf..00000000 --- a/packages/naming.presets/origin-react.js +++ /dev/null @@ -1,13 +0,0 @@ -'use strict'; - -const origin = require('./origin'); - -module.exports = Object.assign({}, origin, { - delims: Object.assign({}, origin.delims, { - elem: '-' - }), - fs: Object.assign({}, origin.fs, { - delims: { elem: '' } - }), - wordPattern: '[a-zA-Z0-9]+' -}); diff --git a/packages/naming.presets/package.json b/packages/naming.presets/package.json index 76339494..6deac5aa 100644 --- a/packages/naming.presets/package.json +++ b/packages/naming.presets/package.json @@ -32,13 +32,18 @@ "engines": { "node": ">= 8.0" }, - "main": "index.js", - "typings": "index.d.ts", + "main": "lib/index.js", + "module": "es/index.js", + "typings": "typings/index.d.ts", "files": [ "*.js", "index.d.ts" ], "scripts": { - "test": "nyc mocha" + "build:cjs": "tsc --module commonjs --outDir lib", + "build:es": "tsc --module es2015 --outDir es", + "build": "npm run clean && npm run build:cjs && npm run build:es", + "clean": "rm -rf lib es typings", + "test": "nyc mocha -r ts-node/register" } } diff --git a/packages/naming.presets/react.js b/packages/naming.presets/react.js deleted file mode 100644 index 11e9cf75..00000000 --- a/packages/naming.presets/react.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict'; - -const base = require('./origin-react'); - -module.exports = Object.assign({}, base, { - fs: Object.assign(base.fs, { - pattern: '${entity}${layer?@${layer}}.${tech}' - }) -}); diff --git a/packages/naming.presets/create.js b/packages/naming.presets/src/create.ts similarity index 62% rename from packages/naming.presets/create.js rename to packages/naming.presets/src/create.ts index 8b2fc27c..115195c6 100644 --- a/packages/naming.presets/create.js +++ b/packages/naming.presets/src/create.ts @@ -1,8 +1,9 @@ -'use strict'; +import * as __presets__ from './'; +import { Presets } from './types'; -var presets = require('.'); - -var DEFAULT_PRESET = 'origin'; +const DEFAULT_PRESET = 'origin'; +// Use alias because namespace not has index signature +const presets: Presets = __presets__; module.exports = init; @@ -13,13 +14,13 @@ module.exports = init; * @param {Object} [userDefaults] - defaults * @returns {INamingConventionDelims} */ -function init(options, userDefaults) { +function init(options: any, userDefaults: any) { if (!options) { return presets[DEFAULT_PRESET]; } if (typeof options === 'string') { - var preset = presets[options]; + const preset = presets[options]; if (!preset) { throw new Error('The `' + options + '` naming is unknown.'); @@ -28,7 +29,7 @@ function init(options, userDefaults) { return preset; } - var defaultPreset = options.preset || DEFAULT_PRESET; + const defaultPreset = options.preset || DEFAULT_PRESET; // TODO: Warn about incorrect preset if (typeof userDefaults === 'string') { @@ -37,11 +38,11 @@ function init(options, userDefaults) { userDefaults = {}; } - var defaults = presets[defaultPreset]; - var defaultDelims = userDefaults.delims || defaults.delims; - var defaultModDelims = userDefaults.mod || defaultDelims.mod; - var optionsDelims = options.delims || {}; - var mod = optionsDelims.mod || defaultModDelims; + const defaults = presets[defaultPreset]; + const defaultDelims = userDefaults.delims || defaults.delims; + const defaultModDelims = userDefaults.mod || defaultDelims.mod; + const optionsDelims = options.delims || {}; + const mod = optionsDelims.mod || defaultModDelims; const res = { delims: { @@ -53,7 +54,7 @@ function init(options, userDefaults) { val: mod.val || defaultModDelims.val } }, - fs: Object.assign({}, defaults.fs, userDefaults.fs, options.fs), + fs: { ...defaults.fs, ...userDefaults.fs, ...options.fs }, wordPattern: options.wordPattern || userDefaults.wordPattern || defaults.wordPattern }; diff --git a/packages/naming.presets/src/index.ts b/packages/naming.presets/src/index.ts new file mode 100644 index 00000000..86cd4efb --- /dev/null +++ b/packages/naming.presets/src/index.ts @@ -0,0 +1,5 @@ +export { origin } from './origin'; +export { legacy } from './legacy'; +export { react } from './react'; +export { originReact } from './origin-react'; +export { twoDashes } from './two-dashes'; diff --git a/packages/naming.presets/src/legacy.ts b/packages/naming.presets/src/legacy.ts new file mode 100644 index 00000000..f04cf151 --- /dev/null +++ b/packages/naming.presets/src/legacy.ts @@ -0,0 +1,10 @@ +import { INamingConvention } from './types'; +import { origin } from './origin'; + +export const legacy: INamingConvention = { + ...origin, + fs: { + ...origin.fs, + pattern: '${entity}${layer?@${layer}}.${tech}' + } +}; diff --git a/packages/naming.presets/src/origin-react.ts b/packages/naming.presets/src/origin-react.ts new file mode 100644 index 00000000..307ef187 --- /dev/null +++ b/packages/naming.presets/src/origin-react.ts @@ -0,0 +1,15 @@ +import { INamingConvention } from './types'; +import { origin } from './origin'; + +export const originReact: INamingConvention = { + ...origin, + delims: { + ...origin.delims, + elem: '-' + }, + fs: { + ...origin.fs, + delims: { elem: '' } + }, + wordPattern: '[a-zA-Z0-9]+' +}; diff --git a/packages/naming.presets/origin.js b/packages/naming.presets/src/origin.ts similarity index 78% rename from packages/naming.presets/origin.js rename to packages/naming.presets/src/origin.ts index bbbf2959..c86c3e80 100644 --- a/packages/naming.presets/origin.js +++ b/packages/naming.presets/src/origin.ts @@ -1,6 +1,6 @@ -'use strict'; +import { INamingConvention } from './types'; -module.exports = { +export const origin: INamingConvention = { delims: { elem: '__', mod: { name: '_', val: '_' } diff --git a/packages/naming.presets/src/react.ts b/packages/naming.presets/src/react.ts new file mode 100644 index 00000000..5097b6a8 --- /dev/null +++ b/packages/naming.presets/src/react.ts @@ -0,0 +1,10 @@ +import { INamingConvention } from './types'; +import { originReact } from './origin-react'; + +export const react: INamingConvention = { + ...originReact, + fs: { + ...originReact.fs, + pattern: '${entity}${layer?@${layer}}.${tech}' + } +}; diff --git a/packages/naming.presets/src/two-dashes.ts b/packages/naming.presets/src/two-dashes.ts new file mode 100644 index 00000000..c1fc39b7 --- /dev/null +++ b/packages/naming.presets/src/two-dashes.ts @@ -0,0 +1,14 @@ +import { INamingConvention } from './types'; + +export const twoDashes: INamingConvention = { + delims: { + elem: '__', + mod: { name: '--', val: '_' } + }, + fs: { + // delims: { elem: '__', mod: '--' }, // redundand because of defaults + pattern: '${layer}.blocks/${entity}.${tech}', + scheme: 'nested' + }, + wordPattern: '[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*' +}; diff --git a/packages/naming.presets/src/types.ts b/packages/naming.presets/src/types.ts new file mode 100644 index 00000000..b968ae14 --- /dev/null +++ b/packages/naming.presets/src/types.ts @@ -0,0 +1,25 @@ +export type Mod = { + name: string; + val: string; +}; + +export interface INamingConventionDelims { + elem: string; + mod?: Mod | string; +} + +export type FileSystem = { + delims?: INamingConventionDelims; + pattern: string; + scheme: string; +}; + +export interface INamingConvention { + delims: INamingConventionDelims; + fs: FileSystem; + wordPattern: string; +} + +export type Presets = { + [key: string]: INamingConvention +}; diff --git a/packages/naming.presets/test/cell.test.js b/packages/naming.presets/test/cell.test.js index fd60c051..9a190f8a 100644 --- a/packages/naming.presets/test/cell.test.js +++ b/packages/naming.presets/test/cell.test.js @@ -7,7 +7,7 @@ const BemEntityName = require('@bem/sdk.entity-name'); const createStringify = require('@bem/sdk.naming.cell.stringify'); -const presets = require('..'); +const presets = require('../lib'); const createPreset = (name, fsConv, conv) => { const res = Object.assign({}, presets[name], conv); @@ -22,8 +22,8 @@ describe('default', () => { const originNested = createStringify(presets.origin); const reactFlat = createStringify(createPreset('react', { scheme: 'flat' })); const reactNested = createStringify(presets.react); - const twoFlat = createStringify(createPreset('two-dashes', { scheme: 'flat' })); - const twoNested = createStringify(presets['two-dashes']); + const twoFlat = createStringify(createPreset('twoDashes', { scheme: 'flat' })); + const twoNested = createStringify(presets['twoDashes']); it('should return path + tech', () => { expect(originNested( diff --git a/packages/naming.presets/tsconfig.json b/packages/naming.presets/tsconfig.json new file mode 100644 index 00000000..cb7d2c70 --- /dev/null +++ b/packages/naming.presets/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../tsconfig.shared", + "compilerOptions": { + "rootDir": "src", + "declaration": true, + "declarationDir": "typings" + } +} diff --git a/packages/naming.presets/two-dashes.js b/packages/naming.presets/two-dashes.js deleted file mode 100644 index b45174ce..00000000 --- a/packages/naming.presets/two-dashes.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict'; - -const origin = require('./origin'); - -module.exports = Object.assign({}, origin, { - delims: { - elem: '__', - mod: { name: '--', val: '_' } - } -}); diff --git a/tsconfig.shared.json b/tsconfig.shared.json new file mode 100644 index 00000000..b289514a --- /dev/null +++ b/tsconfig.shared.json @@ -0,0 +1,17 @@ +{ + "compilerOptions": { + "alwaysStrict": true, + "downlevelIteration": true, + "moduleResolution": "node", + "noFallthroughCasesInSwitch": true, + "noImplicitAny": true, + "noImplicitReturns": true, + "noImplicitThis": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "pretty": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "target": "es5" + } +} diff --git a/tslint.json b/tslint.json index 9e1edaa3..aa91eebb 100644 --- a/tslint.json +++ b/tslint.json @@ -1,3 +1,6 @@ { - "extends": "tslint-config-typings" + "extends": "tslint-config-typings", + "rules": { + "interface-name": [true, "always-prefix"] + } }