From bb308f44f8040c206c217de99a6a09e0abb7ebea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Gabriel=20Lima?= Date: Fri, 29 Jan 2016 14:23:38 -0300 Subject: [PATCH] chore(tools): remove script to generate operator patches This is not being used anymore. --- package.json | 1 - tools/generate-operator-patches.ts | 227 ----------------------------- 2 files changed, 228 deletions(-) delete mode 100644 tools/generate-operator-patches.ts diff --git a/package.json b/package.json index 4c773b9583..acd709f528 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,6 @@ "perf_micro": "node ./perf/micro/index.js", "prepublish": "npm run build_all", "generate_packages": "node .make-packages.js", - "generate_operator_patches": "tsc -outDir dist/ ./tools/generate-operator-patches.ts && node ./dist/generate-operator-patches.js --exec", "commit": "git-cz", "check_circular_dependencies": "madge ./dist/cjs --circular" }, diff --git a/tools/generate-operator-patches.ts b/tools/generate-operator-patches.ts deleted file mode 100644 index c27dde8823..0000000000 --- a/tools/generate-operator-patches.ts +++ /dev/null @@ -1,227 +0,0 @@ -/// -/// -/** - * This file generates the side-effect version of each operator, - * and saves it in src/add by the same path+filename as - * the original. - * - * The side effect version of an operator imports the operator and - * Observable, and attaches the operator to Observable.prototype - * (or Observable if static). - */ - -var fs = require('fs'); -var mkdirp = require('mkdirp'); - -interface OperatorWrapper { - path?: string; - methodType: MethodType; - isExtended?: boolean; - isDom?: boolean; - isStatic?: boolean; - exportedFnName?: string; - exportedClassName?: string; - memberName?: string; - aliases?: string[]; - newFileContents?: string; - srcFileContents?: string; - kitchenSinkFileContents?: string; -} - -enum MethodType { Observable, Operator } - -const MethodTypeDirectories = { - [MethodType.Observable]: 'observable', - [MethodType.Operator]: 'operator' -}; - -/** - * Special operators whose name on the object or prototype is different - * from exported function name. - **/ -const SpecialCasePrototypes = { - 'zip.ts': 'zip', - 'switch.ts': 'switch', - 'do.ts': 'do', - 'finally.ts': 'finally', - 'catch.ts': 'catch', - 'let.ts': 'let' -}; - -const AdditionalAliases = { - 'mergeMap.ts': ['flatMap'], - 'fromArray.ts': ['of'], - 'let.ts': ['letBind'] -}; - -const AliasMethodOverrides = { - 'fromArray.ts': { - of: 'of' - } -}; - -const header = ''; - -/** - * Temporary hack to enable typescript to properly export type definitions. - * More information on: - * - https://github.com/ReactiveX/RxJS/issues/1010 - * - https://github.com/Microsoft/TypeScript/issues/6022 - **/ -const typescriptHack = 'export var _void: void;' - -function generateNewOperatorFileContents (op:OperatorWrapper): OperatorWrapper { - var baseObject = op.isExtended ? 'observableProto' : 'Observable'; - var optPrototype = op.isStatic || op.isExtended ? '' : 'prototype.'; - var imports = `import {Observable} from '../../Observable'; -import {${op.exportedFnName}} from '../../operator/${op.path.replace('.ts','')}'; -${op.isExtended ? 'import {KitchenSinkOperators} from \'../../Rx.KitchenSink\';' + '\n': ''}`; - var extendedProto = 'const observableProto = (>Observable.prototype);' - var patch = op.aliases.map((alias) => { - return `${baseObject}.${optPrototype}${alias} = ${op.exportedFnName};`; - }).join('\n'); - - var contents = `${header} -${imports} -${op.isExtended ? extendedProto + '\n' : ''}${patch} - -${typescriptHack}`; - - return Object.assign({}, op, { - newFileContents: contents - }); -} - -function generateNewObservableFileContents (op:OperatorWrapper): OperatorWrapper { - var overrides = AliasMethodOverrides[op.path]; - var levelsUp = op.isDom ? '../../../' : '../../'; - var imports = `import {Observable} from '${levelsUp}Observable'; -import {${op.exportedClassName}} from '${levelsUp}observable/${op.path.replace('.ts','')}';`; - var patch = op.aliases.map((alias) => { - return `Observable.${alias} = ${op.exportedClassName}.${(overrides && overrides[alias]) || 'create'};`; - }).join('\n'); - - var contents = `${header} -${imports} - -${patch} - -${typescriptHack}`; - - return Object.assign({}, op, { - newFileContents: contents - }); -} - -function checkStatic (op:OperatorWrapper):OperatorWrapper { - return Object.assign({}, op, {isStatic: /\-static\.ts/.test(op.path)}); -} - -function checkExtended (op:OperatorWrapper): OperatorWrapper { - const extended = op.kitchenSinkFileContents.indexOf(op.memberName + '?') > -1; - return Object.assign({}, op, {isExtended: extended}) -} - -function getOperatorName (op:OperatorWrapper): OperatorWrapper { - return Object.assign({}, op, { - exportedFnName: /export function ([A-Z_]*)/i.exec(op.srcFileContents)[1] - }); -} - -function getClassName (op:OperatorWrapper): OperatorWrapper { - return Object.assign({}, op, { - exportedClassName: /export class ([A-Z]*)/i.exec(op.srcFileContents)[1] - }); -} - -function loadSrcFile (op:OperatorWrapper): OperatorWrapper { - return Object.assign({}, op, { - srcFileContents: fs.readFileSync(`./src/${MethodTypeDirectories[op.methodType]}/${op.path}`).toString() - }) -} - -function writeToDisk (op:OperatorWrapper): void { - fs.writeFileSync(`./src/add/${MethodTypeDirectories[op.methodType]}/${op.path}`, op.newFileContents); -} - -function getNameOnOperatorProto (op:OperatorWrapper): OperatorWrapper { - return Object.assign({}, op, { - memberName: SpecialCasePrototypes[op.path] || op.exportedFnName - }); -} - -function getNameOnObservableProto (op:OperatorWrapper): OperatorWrapper { - return Object.assign({}, op, { - memberName: op.path.replace('dom/', '').replace('.ts', '') - }); -} - -function getAliases (op:OperatorWrapper): OperatorWrapper { - return Object.assign({}, op, { - aliases: [op.memberName, ...AdditionalAliases[op.path]].filter(v => !!v) - }); -} - -function checkForCreate (op: OperatorWrapper): boolean { - return /static create/.test(op.srcFileContents); -} - -function checkDom (op: OperatorWrapper): OperatorWrapper { - return Object.assign({}, op, {isDom: /(dom\/)/g.test(op.path)}); -} - -if (process.argv.find((v) => v === '--exec')) { - mkdirp('./src/add/operator', () => { - const kitchenSinkContents = fs.readFileSync('./src/Rx.KitchenSink.ts'); - fs.readdirSync('./src/operator') - .filter(o => o.endsWith('.ts')) - // Create base Operator object - .map(o => { - return { - path: o, - methodType: MethodType.Operator, - kitchenSinkFileContents: kitchenSinkContents - }; - }) - .map(loadSrcFile) - // Only include modules that actually export a function - .filter(op => { - return /export function ([A-Z]*)/i.exec(op.srcFileContents); - }) - /** - * Check if the operator should be static, assuming the path contains -static. - **/ - .map(checkStatic) - // Get the exported function name - .map(getOperatorName) - // Get the default name of the operator as it should appear on Observable - .map(getNameOnOperatorProto) - // Get any special-case aliases that should be applied - .map(getAliases) - // Mark operator as extended, if applicable - .map(checkExtended) - .map(generateNewOperatorFileContents) - .forEach(writeToDisk); - - // Repeat the process for src/observable/* - mkdirp('./src/add/observable/dom', () => { - fs.readdirSync('./src/observable') - .concat(fs.readdirSync('./src/observable/dom').map(o => `dom/${o}`)) - .filter(o => o.endsWith('.ts')) - .map(o => { - return { - path: o, - methodType: MethodType.Observable - } - }) - .map(loadSrcFile) - .filter(checkForCreate) - .map(checkDom) - .map(getClassName) - .map(getNameOnObservableProto) - .map(getAliases) - .map(generateNewObservableFileContents) - .map(writeToDisk); - }); - }); -}