UNPKG

bit-bin

Version:

<a href="https://opensource.org/licenses/Apache-2.0"><img alt="apache" src="https://img.shields.io/badge/License-Apache%202.0-blue.svg"></a> <a href="https://github.com/teambit/bit/blob/master/CONTRIBUTING.md"><img alt="prs" src="https://img.shields.io/b

259 lines (205 loc) 9.17 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; function _defineProperty2() { const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); _defineProperty2 = function () { return data; }; return data; } function path() { const data = _interopRequireWildcard(require("path")); path = function () { return data; }; return data; } function _fsExtra() { const data = _interopRequireDefault(require("fs-extra")); _fsExtra = function () { return data; }; return data; } function _e2eHelper() { const data = require("./e2e-helper"); _e2eHelper = function () { return data; }; return data; } function _utils() { const data = require("../utils"); _utils = function () { return data; }; return data; } class EnvHelper { // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! constructor(command, fsHelper, scopes, scopeHelper, fixtures) { (0, _defineProperty2().default)(this, "command", void 0); (0, _defineProperty2().default)(this, "fs", void 0); (0, _defineProperty2().default)(this, "fixtures", void 0); (0, _defineProperty2().default)(this, "scopes", void 0); (0, _defineProperty2().default)(this, "scopeHelper", void 0); (0, _defineProperty2().default)(this, "compilerCreated", false); (0, _defineProperty2().default)(this, "dummyCompilerCreated", void 0); (0, _defineProperty2().default)(this, "dummyTesterCreated", void 0); this.command = command; this.fs = fsHelper; this.scopes = scopes; this.scopeHelper = scopeHelper; this.fixtures = fixtures; } importCompiler(id) { if (!id) { id = `${this.scopes.env}/compilers/babel`; this.createCompiler(); } return this.command.runCmd(`bit import ${id} --compiler`); } importTypescriptCompiler(version = '3.0.0') { this.fixtures.ensureGlobalRemoteScope(); this.scopeHelper.addGlobalRemoteScope(); return this.importCompiler(`${this.scopes.globalRemote}/compilers/typescript@${version}`); } getTypeScriptSettingsForES5() { return { rawConfig: { tsconfig: { compilerOptions: { target: 'ES5', module: 'CommonJS' } } } }; } importDummyCompiler(dummyType = 'dummy') { const id = `${this.scopes.env}/compilers/dummy`; this.createDummyCompiler(dummyType); return this.command.runCmd(`bit import ${id} --compiler`); } changeDummyCompilerCode(originalCode, replaceTo) { const compilerPath = path().join('.bit/components/compilers/dummy', this.scopes.env, '0.0.1/compiler.js'); const compilerContent = this.fs.readFile(compilerPath); const changedCompiler = compilerContent.replace(originalCode, replaceTo); this.fs.outputFile(compilerPath, changedCompiler); } importDummyTester(dummyType = 'dummy') { const id = `${this.scopes.env}/testers/dummy`; this.createDummyTester(dummyType); return this.command.runCmd(`bit import ${id} --tester`); } importTester(id = `${this.scopes.globalRemote}/testers/mocha@0.0.12`) { this.fixtures.ensureGlobalRemoteScope(); this.scopeHelper.addGlobalRemoteScope(); this.command.runCmd(`bit import ${id} --tester`); } createDummyCompiler(dummyType = 'dummy') { // if (this.dummyCompilerCreated) return this.scope.addRemoteScope(this.scopes.envScopePath); // TODO: this is not really a scope but a workspace const tempScope = `${(0, _utils().generateRandomStr)()}-temp`; const tempScopePath = path().join(this.scopes.e2eDir, tempScope); _fsExtra().default.emptyDirSync(tempScopePath); this.scopeHelper.initWorkspace(tempScopePath); const sourceDir = path().join(this.fixtures.getFixturesDir(), 'compilers', dummyType); const compiler = _fsExtra().default.readFileSync(path().join(sourceDir, 'compiler.js'), 'utf-8'); _fsExtra().default.writeFileSync(path().join(tempScopePath, 'compiler.js'), compiler); this.command.addComponent('compiler.js', { i: 'compilers/dummy' }, tempScopePath); this.command.runCmd('bit tag compilers/dummy -m msg', tempScopePath); _fsExtra().default.emptyDirSync(this.scopes.envPath); this.command.runCmd('bit init --bare', this.scopes.envPath); this.command.runCmd(`bit remote add file://${this.scopes.envPath}`, tempScopePath); this.command.runCmd(`bit export ${this.scopes.env} compilers/dummy`, tempScopePath); this.scopeHelper.addRemoteScope(this.scopes.envPath); this.dummyCompilerCreated = true; return true; } createDummyTester(dummyType) { if (this.dummyTesterCreated) return this.scopeHelper.addRemoteScope(this.scopes.envPath); // TODO: this is not really a scope but a workspace const tempScope = `${(0, _utils().generateRandomStr)()}-temp`; const tempScopePath = path().join(this.scopes.e2eDir, tempScope); _fsExtra().default.emptyDirSync(tempScopePath); this.scopeHelper.initWorkspace(tempScopePath); const sourceDir = path().join(this.fixtures.getFixturesDir(), 'testers', dummyType); const tester = _fsExtra().default.readFileSync(path().join(sourceDir, 'tester.js'), 'utf-8'); _fsExtra().default.writeFileSync(path().join(tempScopePath, 'tester.js'), tester); (0, _e2eHelper().ensureAndWriteJson)(path().join(tempScopePath, 'package.json'), { name: 'dummy-compiler', version: '1.0.0', dependencies: { mocha: '6.1.4', chai: '4.2.0' } }); this.command.runCmd('npm install', tempScopePath); this.command.addComponent('tester.js', { i: 'testers/dummy' }, tempScopePath); this.command.runCmd('bit tag testers/dummy -m msg', tempScopePath); _fsExtra().default.emptyDirSync(this.scopes.envPath); this.command.runCmd('bit init --bare', this.scopes.envPath); this.command.runCmd(`bit remote add file://${this.scopes.envPath}`, tempScopePath); this.command.runCmd(`bit export ${this.scopes.env} testers/dummy`, tempScopePath); this.scopeHelper.addRemoteScope(this.scopes.envPath); this.dummyTesterCreated = true; return true; } createCompiler() { if (this.compilerCreated) return this.scopeHelper.addRemoteScope(this.scopes.envPath); const tempScope = `${(0, _utils().generateRandomStr)()}-temp`; const tempScopePath = path().join(this.scopes.e2eDir, tempScope); _fsExtra().default.emptyDirSync(tempScopePath); this.scopeHelper.initWorkspace(tempScopePath); const sourceDir = path().join(this.fixtures.getFixturesDir(), 'compilers', 'babel'); const compiler = _fsExtra().default.readFileSync(path().join(sourceDir, 'compiler.js'), 'utf-8'); _fsExtra().default.writeFileSync(path().join(tempScopePath, 'compiler.js'), compiler); const babelCorePackageJson = { name: 'babel-core', version: '6.25.0' }; const babelPluginTransformObjectRestSpreadPackageJson = { name: 'babel-plugin-transform-object-rest-spread', version: '6.23.0' }; const babelPresetLatestPackageJson = { name: 'babel-preset-latest', version: '6.24.1' }; const vinylPackageJson = { name: 'vinyl', version: '2.1.0' }; const nodeModulesDir = path().join(tempScopePath, 'node_modules'); (0, _e2eHelper().ensureAndWriteJson)(path().join(nodeModulesDir, 'babel-core', 'package.json'), babelCorePackageJson); (0, _e2eHelper().ensureAndWriteJson)(path().join(nodeModulesDir, 'babel-plugin-transform-object-rest-spread', 'package.json'), babelPluginTransformObjectRestSpreadPackageJson); (0, _e2eHelper().ensureAndWriteJson)(path().join(nodeModulesDir, 'babel-preset-latest', 'package.json'), babelPresetLatestPackageJson); (0, _e2eHelper().ensureAndWriteJson)(path().join(nodeModulesDir, 'vinyl', 'package.json'), vinylPackageJson); (0, _e2eHelper().ensureAndWriteJson)(path().join(nodeModulesDir, 'babel-core', 'index.js'), ''); (0, _e2eHelper().ensureAndWriteJson)(path().join(nodeModulesDir, 'babel-plugin-transform-object-rest-spread', 'index.js'), ''); (0, _e2eHelper().ensureAndWriteJson)(path().join(nodeModulesDir, 'babel-preset-latest', 'index.js'), ''); (0, _e2eHelper().ensureAndWriteJson)(path().join(nodeModulesDir, 'vinyl', 'index.js'), ''); this.command.addComponent('compiler.js', { i: 'compilers/babel' }, tempScopePath); this.command.runCmd('bit tag compilers/babel -m msg', tempScopePath); _fsExtra().default.emptyDirSync(this.scopes.envPath); this.command.runCmd('bit init --bare', this.scopes.envPath); this.command.runCmd(`bit remote add file://${this.scopes.envPath}`, tempScopePath); this.command.runCmd(`bit export ${this.scopes.env} compilers/babel`, tempScopePath); this.scopeHelper.addRemoteScope(this.scopes.envPath); this.compilerCreated = true; return true; } } exports.default = EnvHelper;