@teambit/scripts 0.0.0-00df84d7db07c10ea89ca52e1dfa26599e0b6049
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/exceptions/index.d.ts +1 -0
- package/dist/exceptions/index.js +20 -0
- package/dist/exceptions/index.js.map +1 -0
- package/dist/exceptions/script-not-found.d.ts +6 -0
- package/dist/exceptions/script-not-found.js +23 -0
- package/dist/exceptions/script-not-found.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +48 -0
- package/dist/index.js.map +1 -0
- package/dist/preview-1760387564381.js +7 -0
- package/dist/script-definition.d.ts +12 -0
- package/dist/script-definition.js +3 -0
- package/dist/script-definition.js.map +1 -0
- package/dist/script.cmd.d.ts +20 -0
- package/dist/script.cmd.js +40 -0
- package/dist/script.cmd.js.map +1 -0
- package/dist/scripts.aspect.d.ts +2 -0
- package/dist/scripts.aspect.js +18 -0
- package/dist/scripts.aspect.js.map +1 -0
- package/dist/scripts.d.ts +12 -0
- package/dist/scripts.js +34 -0
- package/dist/scripts.js.map +1 -0
- package/dist/scripts.main.runtime.d.ts +41 -0
- package/dist/scripts.main.runtime.js +284 -0
- package/dist/scripts.main.runtime.js.map +1 -0
- package/dist/scripts.service.d.ts +18 -0
- package/dist/scripts.service.js +53 -0
- package/dist/scripts.service.js.map +1 -0
- package/exceptions/index.ts +1 -0
- package/exceptions/script-not-found.ts +10 -0
- package/index.ts +8 -0
- package/package.json +45 -0
- package/script-definition.ts +16 -0
- package/script.cmd.ts +40 -0
- package/scripts.aspect.ts +5 -0
- package/scripts.main.runtime.ts +255 -0
- package/scripts.service.ts +54 -0
- package/scripts.ts +32 -0
- package/types/asset.d.ts +29 -0
- package/types/style.d.ts +42 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_cli","data","require","_component","_envs","_logger","_workspace","_chalk","_interopRequireDefault","_lodash","_child_process","_scripts","_scripts2","_script","_exceptions","e","__esModule","default","_defineProperty","r","t","_toPropertyKey","Object","defineProperty","value","enumerable","configurable","writable","i","_toPrimitive","Symbol","toPrimitive","call","TypeError","String","Number","ScriptsMain","constructor","workspace","envs","logger","componentAspect","config","getConfigErrorMessage","chalk","yellow","runScript","scriptName","allowedEnvs","length","components","getComponents","componentsByEnv","groupComponentsByEnv","results","envId","envComponents","entries","env","getEnvDefinitionByStringId","scripts","getScriptsFromEnv","has","ScriptNotFound","handler","get","title","green","push","result","executeScript","join","listAllScripts","foundAnyScripts","isEmpty","cyan","gray","scriptsList","list","forEach","handlerStr","white","bold","isEnvAllowed","some","allowedEnv","includes","startsWith","host","getHost","Error","grouped","groupBy","component","getOrCalculateEnv","id","filtered","getScripts","undefined","executeShellCommand","executeFunction","command","output","execSync","cwd","path","encoding","stdio","error","errorMsg","stderr","message","red","context","provider","cli","loggerMain","createLogger","ScriptsAspect","scriptsService","ScriptsService","scriptsMain","registerService","scriptCmd","ScriptCmd","register","exports","CLIAspect","WorkspaceAspect","EnvsAspect","ComponentAspect","LoggerAspect","MainRuntime","addRuntime","_default"],"sources":["scripts.main.runtime.ts"],"sourcesContent":["import { CLIAspect, CLIMain, MainRuntime } from '@teambit/cli';\nimport type { Component } from '@teambit/component';\nimport { ComponentAspect, ComponentMain } from '@teambit/component';\nimport type { EnvsMain, EnvDefinition } from '@teambit/envs';\nimport { EnvsAspect } from '@teambit/envs';\nimport type { Logger, LoggerMain } from '@teambit/logger';\nimport { LoggerAspect } from '@teambit/logger';\nimport type { Workspace } from '@teambit/workspace';\nimport { WorkspaceAspect } from '@teambit/workspace';\nimport chalk from 'chalk';\nimport { groupBy } from 'lodash';\nimport { execSync } from 'child_process';\nimport { ScriptsAspect } from './scripts.aspect';\nimport { ScriptsService } from './scripts.service';\nimport { ScriptCmd } from './script.cmd';\nimport { ScriptNotFound } from './exceptions';\nimport type { Scripts } from './scripts';\nimport type { ScriptHandler, ScriptExecuteContext } from './script-definition';\n\nexport interface ScriptsConfig {\n envs?: string[];\n}\n\nexport class ScriptsMain {\n constructor(\n private workspace: Workspace,\n private envs: EnvsMain,\n private logger: Logger,\n private componentAspect: ComponentMain,\n private config: ScriptsConfig\n ) {}\n\n private getConfigErrorMessage(): string {\n return chalk.yellow(\n 'no envs configured. Add to workspace.jsonc:\\n' +\n '{\\n' +\n ' \"teambit.workspace/scripts\": {\\n' +\n ' \"envs\": [\"your-scope/your-env\"]\\n' +\n ' }\\n' +\n '}'\n );\n }\n\n /**\n * Run a script for all components\n */\n async runScript(scriptName: string): Promise<string> {\n // Filter envs based on config\n const allowedEnvs = this.config.envs || [];\n if (allowedEnvs.length === 0) {\n return this.getConfigErrorMessage();\n }\n\n const components = await this.getComponents();\n if (!components.length) {\n return chalk.yellow('no components found');\n }\n\n // Group components by environment, filtering only configured envs\n const componentsByEnv = this.groupComponentsByEnv(components, allowedEnvs);\n const results: string[] = [];\n\n for (const [envId, envComponents] of Object.entries(componentsByEnv)) {\n const env = this.envs.getEnvDefinitionByStringId(envId);\n if (!env) continue;\n\n const scripts = this.getScriptsFromEnv(env);\n if (!scripts) continue;\n\n if (!scripts.has(scriptName)) {\n throw new ScriptNotFound(scriptName, envId);\n }\n\n const handler = scripts.get(scriptName);\n if (!handler) continue;\n\n const title = chalk.green(\n `\\nRunning script \"${scriptName}\" for ${envComponents.length} component(s) with env ${envId}:`\n );\n results.push(title);\n\n const result = await this.executeScript(handler, envComponents);\n results.push(result);\n }\n\n return results.join('\\n');\n }\n\n /**\n * List all available scripts from all environments\n */\n async listAllScripts(): Promise<string> {\n // Filter envs based on config\n const allowedEnvs = this.config.envs || [];\n if (allowedEnvs.length === 0) {\n return this.getConfigErrorMessage();\n }\n\n const components = await this.getComponents();\n if (!components.length) {\n return chalk.yellow('no components found');\n }\n\n // Group components by environment, filtering only configured envs\n const componentsByEnv = this.groupComponentsByEnv(components, allowedEnvs);\n const results: string[] = [];\n let foundAnyScripts = false;\n\n for (const [envId, envComponents] of Object.entries(componentsByEnv)) {\n const env = this.envs.getEnvDefinitionByStringId(envId);\n if (!env) continue;\n\n const scripts = this.getScriptsFromEnv(env);\n if (!scripts || scripts.isEmpty()) {\n continue;\n }\n\n if (!foundAnyScripts) {\n results.push(chalk.green('Available scripts:\\n'));\n foundAnyScripts = true;\n }\n\n results.push(chalk.cyan(`\\nEnvironment: ${envId}`));\n results.push(chalk.gray(` (used by ${envComponents.length} component(s))`));\n\n const scriptsList = scripts.list();\n scriptsList.forEach((scriptName) => {\n const handler = scripts.get(scriptName);\n const handlerStr = typeof handler === 'function' ? chalk.gray('[function]') : chalk.white(handler as string);\n results.push(` ${chalk.bold(scriptName)}: ${handlerStr}`);\n });\n }\n\n if (!foundAnyScripts) {\n return chalk.yellow('no scripts defined in the configured environments');\n }\n\n return results.join('\\n');\n }\n\n /**\n * Check if an env is allowed to run scripts based on config\n * Supports exact match with or without version\n */\n private isEnvAllowed(envId: string, allowedEnvs: string[]): boolean {\n return allowedEnvs.some((allowedEnv) => {\n // Exact match (with version): \"my-scope/my-env@1.0.0\" === \"my-scope/my-env@1.0.0\"\n if (envId === allowedEnv) return true;\n\n // If config has no version, match env without version part\n // Config: \"my-scope/my-env\" should match \"my-scope/my-env@1.0.0\"\n if (!allowedEnv.includes('@') && envId.startsWith(allowedEnv + '@')) {\n return true;\n }\n\n return false;\n });\n }\n\n private async getComponents(): Promise<Component[]> {\n const host = this.componentAspect.getHost();\n if (!host) throw new Error('workspace not found');\n return host.list();\n }\n\n private groupComponentsByEnv(components: Component[], allowedEnvs?: string[]): Record<string, Component[]> {\n const grouped = groupBy(components, (component) => {\n const env = this.envs.getOrCalculateEnv(component);\n return env.id;\n });\n\n // If allowedEnvs is provided, filter out envs not in the list\n if (allowedEnvs && allowedEnvs.length > 0) {\n const filtered: Record<string, Component[]> = {};\n for (const [envId, envComponents] of Object.entries(grouped)) {\n if (this.isEnvAllowed(envId, allowedEnvs)) {\n filtered[envId] = envComponents;\n }\n }\n return filtered;\n }\n\n return grouped;\n }\n\n private getScriptsFromEnv(env: EnvDefinition): Scripts | undefined {\n if (!env.env.getScripts) return undefined;\n return env.env.getScripts();\n }\n\n private async executeScript(handler: ScriptHandler, components: Component[]): Promise<string> {\n if (typeof handler === 'string') {\n // Execute shell command\n return this.executeShellCommand(handler);\n }\n\n // Execute function\n return this.executeFunction(handler, components);\n }\n\n private executeShellCommand(command: string): string {\n try {\n const output = execSync(command, {\n cwd: this.workspace.path,\n encoding: 'utf-8',\n stdio: 'pipe',\n });\n return chalk.white(output);\n } catch (error: any) {\n const errorMsg = error.stderr || error.message || 'unknown error';\n return chalk.red(`Error executing script: ${errorMsg}`);\n }\n }\n\n private async executeFunction(\n handler: (context?: ScriptExecuteContext) => void | Promise<void>,\n components: Component[]\n ): Promise<string> {\n try {\n const context: ScriptExecuteContext = { components };\n await handler(context);\n return chalk.green('✓ Script function executed successfully');\n } catch (error: any) {\n return chalk.red(`Error executing script function: ${error.message}`);\n }\n }\n\n static slots = [];\n\n static dependencies = [CLIAspect, WorkspaceAspect, EnvsAspect, ComponentAspect, LoggerAspect];\n\n static runtime = MainRuntime;\n\n static async provider(\n [cli, workspace, envs, componentAspect, loggerMain]: [CLIMain, Workspace, EnvsMain, ComponentMain, LoggerMain],\n config: ScriptsConfig\n ) {\n const logger = loggerMain.createLogger(ScriptsAspect.id);\n const scriptsService = new ScriptsService();\n const scriptsMain = new ScriptsMain(workspace, envs, logger, componentAspect, config);\n\n // Register service with envs\n envs.registerService(scriptsService);\n\n // Register CLI command\n const scriptCmd = new ScriptCmd(scriptsMain);\n cli.register(scriptCmd);\n\n return scriptsMain;\n }\n}\n\nScriptsAspect.addRuntime(ScriptsMain);\n\nexport default ScriptsMain;\n"],"mappings":";;;;;;AAAA,SAAAA,KAAA;EAAA,MAAAC,IAAA,GAAAC,OAAA;EAAAF,IAAA,YAAAA,CAAA;IAAA,OAAAC,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AAEA,SAAAE,WAAA;EAAA,MAAAF,IAAA,GAAAC,OAAA;EAAAC,UAA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,GAAG,IAAIrF,WAAW,CAACE,SAAS,EAAEC,IAAI,EAAEC,MAAM,EAAEC,eAAe,EAAEC,MAAM,CAAC;;IAErF;IACAH,IAAI,CAACmF,eAAe,CAACH,cAAc,CAAC;;IAEpC;IACA,MAAMI,SAAS,GAAG,KAAIC,mBAAS,EAACH,WAAW,CAAC;IAC5CN,GAAG,CAACU,QAAQ,CAACF,SAAS,CAAC;IAEvB,OAAOF,WAAW;EACpB;AACF;AAACK,OAAA,CAAA1F,WAAA,GAAAA,WAAA;AAAAlB,eAAA,CAnOYkB,WAAW,WA4MP,EAAE;AAAAlB,eAAA,CA5MNkB,WAAW,kBA8MA,CAAC2F,gBAAS,EAAEC,4BAAe,EAAEC,kBAAU,EAAEC,4BAAe,EAAEC,sBAAY,CAAC;AAAAjH,eAAA,CA9MlFkB,WAAW,aAgNLgG,kBAAW;AAqB9Bd,wBAAa,CAACe,UAAU,CAACjG,WAAW,CAAC;AAAC,IAAAkG,QAAA,GAAAR,OAAA,CAAA7G,OAAA,GAEvBmB,WAAW","ignoreList":[]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { EnvService, EnvDefinition, Env, EnvContext, ServiceTransformationMap } from '@teambit/envs';
|
|
2
|
+
import type { Scripts } from './scripts';
|
|
3
|
+
import type { ScriptsMap } from './script-definition';
|
|
4
|
+
export type ScriptsDescriptor = {
|
|
5
|
+
id: string;
|
|
6
|
+
displayName: string;
|
|
7
|
+
scripts: ScriptsMap;
|
|
8
|
+
};
|
|
9
|
+
type ScriptsTransformationMap = ServiceTransformationMap & {
|
|
10
|
+
getScripts: () => Scripts;
|
|
11
|
+
};
|
|
12
|
+
export declare class ScriptsService implements EnvService<{}, ScriptsDescriptor> {
|
|
13
|
+
name: string;
|
|
14
|
+
render(env: EnvDefinition): Promise<string>;
|
|
15
|
+
getDescriptor(env: EnvDefinition): Promise<ScriptsDescriptor | undefined>;
|
|
16
|
+
transform(env: Env, context: EnvContext): ScriptsTransformationMap | undefined;
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.ScriptsService = void 0;
|
|
7
|
+
function _chalk() {
|
|
8
|
+
const data = _interopRequireDefault(require("chalk"));
|
|
9
|
+
_chalk = function () {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
15
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
16
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|
17
|
+
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
18
|
+
class ScriptsService {
|
|
19
|
+
constructor() {
|
|
20
|
+
_defineProperty(this, "name", 'Scripts');
|
|
21
|
+
}
|
|
22
|
+
async render(env) {
|
|
23
|
+
const descriptor = await this.getDescriptor(env);
|
|
24
|
+
if (!descriptor || Object.keys(descriptor.scripts).length === 0) {
|
|
25
|
+
return '';
|
|
26
|
+
}
|
|
27
|
+
const title = _chalk().default.green('available scripts:');
|
|
28
|
+
const scriptsList = Object.entries(descriptor.scripts).map(([name, handler]) => {
|
|
29
|
+
const handlerStr = typeof handler === 'function' ? _chalk().default.gray('[function]') : _chalk().default.cyan(handler);
|
|
30
|
+
return ` ${_chalk().default.bold(name)}: ${handlerStr}`;
|
|
31
|
+
}).join('\n');
|
|
32
|
+
return `${title}\n${scriptsList}`;
|
|
33
|
+
}
|
|
34
|
+
async getDescriptor(env) {
|
|
35
|
+
if (!env.env.getScripts) return undefined;
|
|
36
|
+
const scripts = env.env.getScripts();
|
|
37
|
+
if (!scripts) return undefined;
|
|
38
|
+
return {
|
|
39
|
+
id: this.name,
|
|
40
|
+
displayName: this.name,
|
|
41
|
+
scripts: scripts.getAll()
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
transform(env, context) {
|
|
45
|
+
if (!(env !== null && env !== void 0 && env.scripts)) return undefined;
|
|
46
|
+
return {
|
|
47
|
+
getScripts: () => env.scripts()(context)
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.ScriptsService = ScriptsService;
|
|
52
|
+
|
|
53
|
+
//# sourceMappingURL=scripts.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_chalk","data","_interopRequireDefault","require","e","__esModule","default","_defineProperty","r","t","_toPropertyKey","Object","defineProperty","value","enumerable","configurable","writable","i","_toPrimitive","Symbol","toPrimitive","call","TypeError","String","Number","ScriptsService","constructor","render","env","descriptor","getDescriptor","keys","scripts","length","title","chalk","green","scriptsList","entries","map","name","handler","handlerStr","gray","cyan","bold","join","getScripts","undefined","id","displayName","getAll","transform","context","exports"],"sources":["scripts.service.ts"],"sourcesContent":["import type { EnvService, EnvDefinition, Env, EnvContext, ServiceTransformationMap } from '@teambit/envs';\nimport chalk from 'chalk';\nimport type { Scripts } from './scripts';\nimport type { ScriptsMap } from './script-definition';\n\nexport type ScriptsDescriptor = {\n id: string;\n displayName: string;\n scripts: ScriptsMap;\n};\n\ntype ScriptsTransformationMap = ServiceTransformationMap & {\n getScripts: () => Scripts;\n};\n\nexport class ScriptsService implements EnvService<{}, ScriptsDescriptor> {\n name = 'Scripts';\n\n async render(env: EnvDefinition) {\n const descriptor = await this.getDescriptor(env);\n if (!descriptor || Object.keys(descriptor.scripts).length === 0) {\n return '';\n }\n\n const title = chalk.green('available scripts:');\n const scriptsList = Object.entries(descriptor.scripts)\n .map(([name, handler]) => {\n const handlerStr = typeof handler === 'function' ? chalk.gray('[function]') : chalk.cyan(handler);\n return ` ${chalk.bold(name)}: ${handlerStr}`;\n })\n .join('\\n');\n\n return `${title}\\n${scriptsList}`;\n }\n\n async getDescriptor(env: EnvDefinition): Promise<ScriptsDescriptor | undefined> {\n if (!env.env.getScripts) return undefined;\n const scripts = env.env.getScripts();\n if (!scripts) return undefined;\n\n return {\n id: this.name,\n displayName: this.name,\n scripts: scripts.getAll(),\n };\n }\n\n transform(env: Env, context: EnvContext): ScriptsTransformationMap | undefined {\n if (!env?.scripts) return undefined;\n return {\n getScripts: () => env.scripts()(context),\n };\n }\n}\n"],"mappings":";;;;;;AACA,SAAAA,OAAA;EAAA,MAAAC,IAAA,GAAAC,sBAAA,CAAAC,OAAA;EAAAH,MAAA,YAAAA,CAAA;IAAA,OAAAC,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AAA0B,SAAAC,uBAAAE,CAAA,WAAAA,CAAA,IAAAA,CAAA,CAAAC,UAAA,GAAAD,CAAA,KAAAE,OAAA,EAAAF,CAAA;AAAA,SAAAG,gBAAAH,CAAA,EAAAI,CAAA,EAAAC,CAAA,YAAAD,CAAA,GAAAE,cAAA,CAAAF,CAAA,MAAAJ,CAAA,GAAAO,MAAA,CAAAC,cAAA,CAAAR,CAAA,EAAAI,CAAA,IAAAK,KAAA,EAAAJ,CAAA,EAAAK,UAAA,MAAAC,YAAA,MAAAC,QAAA,UAAAZ,CAAA,CAAAI,CAAA,IAAAC,CAAA,EAAAL,CAAA;AAAA,SAAAM,eAAAD,CAAA,QAAAQ,CAAA,GAAAC,YAAA,CAAAT,CAAA,uCAAAQ,CAAA,GAAAA,CAAA,GAAAA,CAAA;AAAA,SAAAC,aAAAT,CAAA,EAAAD,CAAA,2BAAAC,CAAA,KAAAA,CAAA,SAAAA,CAAA,MAAAL,CAAA,GAAAK,CAAA,CAAAU,MAAA,CAAAC,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,OAAO,EAAE,OAAOgB,SAAS;IAE9B,OAAO;MACLC,EAAE,EAAE,IAAI,CAACT,IAAI;MACbU,WAAW,EAAE,IAAI,CAACV,IAAI;MACtBR,OAAO,EAAEA,OAAO,CAACmB,MAAM,CAAC;IAC1B,CAAC;EACH;EAEAC,SAASA,CAACxB,GAAQ,EAAEyB,OAAmB,EAAwC;IAC7E,IAAI,EAACzB,GAAG,aAAHA,GAAG,eAAHA,GAAG,CAAEI,OAAO,GAAE,OAAOgB,SAAS;IACnC,OAAO;MACLD,UAAU,EAAEA,CAAA,KAAMnB,GAAG,CAACI,OAAO,CAAC,CAAC,CAACqB,OAAO;IACzC,CAAC;EACH;AACF;AAACC,OAAA,CAAA7B,cAAA,GAAAA,cAAA","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { ScriptNotFound } from './script-not-found';
|
package/index.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ScriptsAspect } from './scripts.aspect';
|
|
2
|
+
|
|
3
|
+
export type { ScriptsMain } from './scripts.main.runtime';
|
|
4
|
+
export { Scripts } from './scripts';
|
|
5
|
+
export type { ScriptHandler, ScriptDefinition, ScriptsMap } from './script-definition';
|
|
6
|
+
export { ScriptNotFound } from './exceptions';
|
|
7
|
+
export default ScriptsAspect;
|
|
8
|
+
export { ScriptsAspect };
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@teambit/scripts",
|
|
3
|
+
"version": "0.0.0-00df84d7db07c10ea89ca52e1dfa26599e0b6049",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"componentId": {
|
|
6
|
+
"name": "scripts",
|
|
7
|
+
"version": "00df84d7db07c10ea89ca52e1dfa26599e0b6049",
|
|
8
|
+
"scope": "teambit.workspace"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"chalk": "4.1.2",
|
|
12
|
+
"lodash": "4.17.21",
|
|
13
|
+
"core-js": "^3.0.0",
|
|
14
|
+
"@babel/runtime": "7.20.0",
|
|
15
|
+
"@teambit/cli": "0.0.1282",
|
|
16
|
+
"@teambit/harmony": "0.4.7",
|
|
17
|
+
"@teambit/logger": "0.0.1375",
|
|
18
|
+
"@teambit/bit-error": "0.0.404",
|
|
19
|
+
"@teambit/component": "0.0.0-91df3f08f06d169db29942bfd5683d9bc7044915",
|
|
20
|
+
"@teambit/envs": "0.0.0-34f2c25fa49da7521e9750365e79ae37c360fe27",
|
|
21
|
+
"@teambit/workspace": "0.0.0-4f368b4b7ac15df3a6eca038a793aeac47a195f8"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@types/lodash": "4.14.165",
|
|
25
|
+
"@types/node": "12.20.4",
|
|
26
|
+
"@types/react": "^17.0.8",
|
|
27
|
+
"@types/react-dom": "^17.0.5",
|
|
28
|
+
"@types/jest": "^26.0.0",
|
|
29
|
+
"@types/testing-library__jest-dom": "5.9.5"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"react": "^16.8.0 || ^17.0.0",
|
|
33
|
+
"react-dom": "^16.8.0 || ^17.0.0"
|
|
34
|
+
},
|
|
35
|
+
"license": "SEE LICENSE IN UNLICENSED",
|
|
36
|
+
"optionalDependencies": {},
|
|
37
|
+
"peerDependenciesMeta": {},
|
|
38
|
+
"exports": {
|
|
39
|
+
"node": {
|
|
40
|
+
"require": "./dist/index.js",
|
|
41
|
+
"import": "./dist/esm.mjs"
|
|
42
|
+
},
|
|
43
|
+
"default": "./dist/index.js"
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Component } from '@teambit/component';
|
|
2
|
+
|
|
3
|
+
export interface ScriptExecuteContext {
|
|
4
|
+
components: Component[];
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export type ScriptHandler = string | ((context?: ScriptExecuteContext) => void | Promise<void>);
|
|
8
|
+
|
|
9
|
+
export interface ScriptDefinition {
|
|
10
|
+
name: string;
|
|
11
|
+
handler: ScriptHandler;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface ScriptsMap {
|
|
15
|
+
[scriptName: string]: ScriptHandler;
|
|
16
|
+
}
|
package/script.cmd.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Command, CommandOptions } from '@teambit/cli';
|
|
2
|
+
import type { ScriptsMain } from './scripts.main.runtime';
|
|
3
|
+
|
|
4
|
+
export type ScriptOptions = {
|
|
5
|
+
list?: boolean;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export class ScriptCmd implements Command {
|
|
9
|
+
name = 'script [script-name]';
|
|
10
|
+
description = 'run a script defined by the environment';
|
|
11
|
+
extendedDescription = `executes custom scripts defined by component environments.
|
|
12
|
+
scripts can be shell commands or JavaScript functions defined in env.scripts().
|
|
13
|
+
runs the script for all components grouped by their environment.
|
|
14
|
+
use --list to see all available scripts.`;
|
|
15
|
+
arguments = [
|
|
16
|
+
{
|
|
17
|
+
name: 'script-name',
|
|
18
|
+
description: 'the name of the script to run (e.g., "generate-svg", "pre-snap")',
|
|
19
|
+
},
|
|
20
|
+
];
|
|
21
|
+
alias = '';
|
|
22
|
+
group = 'development';
|
|
23
|
+
options = [['l', 'list', 'list all available scripts from all environments']] as CommandOptions;
|
|
24
|
+
|
|
25
|
+
constructor(private scripts: ScriptsMain) {}
|
|
26
|
+
|
|
27
|
+
async report(args: string[], options: ScriptOptions): Promise<string> {
|
|
28
|
+
const [scriptName] = args;
|
|
29
|
+
|
|
30
|
+
if (options.list) {
|
|
31
|
+
return this.scripts.listAllScripts();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (!scriptName) {
|
|
35
|
+
throw new Error('script name is required. Use --list to see available scripts.');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return this.scripts.runScript(scriptName);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { CLIAspect, CLIMain, MainRuntime } from '@teambit/cli';
|
|
2
|
+
import type { Component } from '@teambit/component';
|
|
3
|
+
import { ComponentAspect, ComponentMain } from '@teambit/component';
|
|
4
|
+
import type { EnvsMain, EnvDefinition } from '@teambit/envs';
|
|
5
|
+
import { EnvsAspect } from '@teambit/envs';
|
|
6
|
+
import type { Logger, LoggerMain } from '@teambit/logger';
|
|
7
|
+
import { LoggerAspect } from '@teambit/logger';
|
|
8
|
+
import type { Workspace } from '@teambit/workspace';
|
|
9
|
+
import { WorkspaceAspect } from '@teambit/workspace';
|
|
10
|
+
import chalk from 'chalk';
|
|
11
|
+
import { groupBy } from 'lodash';
|
|
12
|
+
import { execSync } from 'child_process';
|
|
13
|
+
import { ScriptsAspect } from './scripts.aspect';
|
|
14
|
+
import { ScriptsService } from './scripts.service';
|
|
15
|
+
import { ScriptCmd } from './script.cmd';
|
|
16
|
+
import { ScriptNotFound } from './exceptions';
|
|
17
|
+
import type { Scripts } from './scripts';
|
|
18
|
+
import type { ScriptHandler, ScriptExecuteContext } from './script-definition';
|
|
19
|
+
|
|
20
|
+
export interface ScriptsConfig {
|
|
21
|
+
envs?: string[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class ScriptsMain {
|
|
25
|
+
constructor(
|
|
26
|
+
private workspace: Workspace,
|
|
27
|
+
private envs: EnvsMain,
|
|
28
|
+
private logger: Logger,
|
|
29
|
+
private componentAspect: ComponentMain,
|
|
30
|
+
private config: ScriptsConfig
|
|
31
|
+
) {}
|
|
32
|
+
|
|
33
|
+
private getConfigErrorMessage(): string {
|
|
34
|
+
return chalk.yellow(
|
|
35
|
+
'no envs configured. Add to workspace.jsonc:\n' +
|
|
36
|
+
'{\n' +
|
|
37
|
+
' "teambit.workspace/scripts": {\n' +
|
|
38
|
+
' "envs": ["your-scope/your-env"]\n' +
|
|
39
|
+
' }\n' +
|
|
40
|
+
'}'
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Run a script for all components
|
|
46
|
+
*/
|
|
47
|
+
async runScript(scriptName: string): Promise<string> {
|
|
48
|
+
// Filter envs based on config
|
|
49
|
+
const allowedEnvs = this.config.envs || [];
|
|
50
|
+
if (allowedEnvs.length === 0) {
|
|
51
|
+
return this.getConfigErrorMessage();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const components = await this.getComponents();
|
|
55
|
+
if (!components.length) {
|
|
56
|
+
return chalk.yellow('no components found');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Group components by environment, filtering only configured envs
|
|
60
|
+
const componentsByEnv = this.groupComponentsByEnv(components, allowedEnvs);
|
|
61
|
+
const results: string[] = [];
|
|
62
|
+
|
|
63
|
+
for (const [envId, envComponents] of Object.entries(componentsByEnv)) {
|
|
64
|
+
const env = this.envs.getEnvDefinitionByStringId(envId);
|
|
65
|
+
if (!env) continue;
|
|
66
|
+
|
|
67
|
+
const scripts = this.getScriptsFromEnv(env);
|
|
68
|
+
if (!scripts) continue;
|
|
69
|
+
|
|
70
|
+
if (!scripts.has(scriptName)) {
|
|
71
|
+
throw new ScriptNotFound(scriptName, envId);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const handler = scripts.get(scriptName);
|
|
75
|
+
if (!handler) continue;
|
|
76
|
+
|
|
77
|
+
const title = chalk.green(
|
|
78
|
+
`\nRunning script "${scriptName}" for ${envComponents.length} component(s) with env ${envId}:`
|
|
79
|
+
);
|
|
80
|
+
results.push(title);
|
|
81
|
+
|
|
82
|
+
const result = await this.executeScript(handler, envComponents);
|
|
83
|
+
results.push(result);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return results.join('\n');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* List all available scripts from all environments
|
|
91
|
+
*/
|
|
92
|
+
async listAllScripts(): Promise<string> {
|
|
93
|
+
// Filter envs based on config
|
|
94
|
+
const allowedEnvs = this.config.envs || [];
|
|
95
|
+
if (allowedEnvs.length === 0) {
|
|
96
|
+
return this.getConfigErrorMessage();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const components = await this.getComponents();
|
|
100
|
+
if (!components.length) {
|
|
101
|
+
return chalk.yellow('no components found');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Group components by environment, filtering only configured envs
|
|
105
|
+
const componentsByEnv = this.groupComponentsByEnv(components, allowedEnvs);
|
|
106
|
+
const results: string[] = [];
|
|
107
|
+
let foundAnyScripts = false;
|
|
108
|
+
|
|
109
|
+
for (const [envId, envComponents] of Object.entries(componentsByEnv)) {
|
|
110
|
+
const env = this.envs.getEnvDefinitionByStringId(envId);
|
|
111
|
+
if (!env) continue;
|
|
112
|
+
|
|
113
|
+
const scripts = this.getScriptsFromEnv(env);
|
|
114
|
+
if (!scripts || scripts.isEmpty()) {
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (!foundAnyScripts) {
|
|
119
|
+
results.push(chalk.green('Available scripts:\n'));
|
|
120
|
+
foundAnyScripts = true;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
results.push(chalk.cyan(`\nEnvironment: ${envId}`));
|
|
124
|
+
results.push(chalk.gray(` (used by ${envComponents.length} component(s))`));
|
|
125
|
+
|
|
126
|
+
const scriptsList = scripts.list();
|
|
127
|
+
scriptsList.forEach((scriptName) => {
|
|
128
|
+
const handler = scripts.get(scriptName);
|
|
129
|
+
const handlerStr = typeof handler === 'function' ? chalk.gray('[function]') : chalk.white(handler as string);
|
|
130
|
+
results.push(` ${chalk.bold(scriptName)}: ${handlerStr}`);
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (!foundAnyScripts) {
|
|
135
|
+
return chalk.yellow('no scripts defined in the configured environments');
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return results.join('\n');
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Check if an env is allowed to run scripts based on config
|
|
143
|
+
* Supports exact match with or without version
|
|
144
|
+
*/
|
|
145
|
+
private isEnvAllowed(envId: string, allowedEnvs: string[]): boolean {
|
|
146
|
+
return allowedEnvs.some((allowedEnv) => {
|
|
147
|
+
// Exact match (with version): "my-scope/my-env@1.0.0" === "my-scope/my-env@1.0.0"
|
|
148
|
+
if (envId === allowedEnv) return true;
|
|
149
|
+
|
|
150
|
+
// If config has no version, match env without version part
|
|
151
|
+
// Config: "my-scope/my-env" should match "my-scope/my-env@1.0.0"
|
|
152
|
+
if (!allowedEnv.includes('@') && envId.startsWith(allowedEnv + '@')) {
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return false;
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
private async getComponents(): Promise<Component[]> {
|
|
161
|
+
const host = this.componentAspect.getHost();
|
|
162
|
+
if (!host) throw new Error('workspace not found');
|
|
163
|
+
return host.list();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
private groupComponentsByEnv(components: Component[], allowedEnvs?: string[]): Record<string, Component[]> {
|
|
167
|
+
const grouped = groupBy(components, (component) => {
|
|
168
|
+
const env = this.envs.getOrCalculateEnv(component);
|
|
169
|
+
return env.id;
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
// If allowedEnvs is provided, filter out envs not in the list
|
|
173
|
+
if (allowedEnvs && allowedEnvs.length > 0) {
|
|
174
|
+
const filtered: Record<string, Component[]> = {};
|
|
175
|
+
for (const [envId, envComponents] of Object.entries(grouped)) {
|
|
176
|
+
if (this.isEnvAllowed(envId, allowedEnvs)) {
|
|
177
|
+
filtered[envId] = envComponents;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return filtered;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return grouped;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
private getScriptsFromEnv(env: EnvDefinition): Scripts | undefined {
|
|
187
|
+
if (!env.env.getScripts) return undefined;
|
|
188
|
+
return env.env.getScripts();
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
private async executeScript(handler: ScriptHandler, components: Component[]): Promise<string> {
|
|
192
|
+
if (typeof handler === 'string') {
|
|
193
|
+
// Execute shell command
|
|
194
|
+
return this.executeShellCommand(handler);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Execute function
|
|
198
|
+
return this.executeFunction(handler, components);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
private executeShellCommand(command: string): string {
|
|
202
|
+
try {
|
|
203
|
+
const output = execSync(command, {
|
|
204
|
+
cwd: this.workspace.path,
|
|
205
|
+
encoding: 'utf-8',
|
|
206
|
+
stdio: 'pipe',
|
|
207
|
+
});
|
|
208
|
+
return chalk.white(output);
|
|
209
|
+
} catch (error: any) {
|
|
210
|
+
const errorMsg = error.stderr || error.message || 'unknown error';
|
|
211
|
+
return chalk.red(`Error executing script: ${errorMsg}`);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
private async executeFunction(
|
|
216
|
+
handler: (context?: ScriptExecuteContext) => void | Promise<void>,
|
|
217
|
+
components: Component[]
|
|
218
|
+
): Promise<string> {
|
|
219
|
+
try {
|
|
220
|
+
const context: ScriptExecuteContext = { components };
|
|
221
|
+
await handler(context);
|
|
222
|
+
return chalk.green('✓ Script function executed successfully');
|
|
223
|
+
} catch (error: any) {
|
|
224
|
+
return chalk.red(`Error executing script function: ${error.message}`);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
static slots = [];
|
|
229
|
+
|
|
230
|
+
static dependencies = [CLIAspect, WorkspaceAspect, EnvsAspect, ComponentAspect, LoggerAspect];
|
|
231
|
+
|
|
232
|
+
static runtime = MainRuntime;
|
|
233
|
+
|
|
234
|
+
static async provider(
|
|
235
|
+
[cli, workspace, envs, componentAspect, loggerMain]: [CLIMain, Workspace, EnvsMain, ComponentMain, LoggerMain],
|
|
236
|
+
config: ScriptsConfig
|
|
237
|
+
) {
|
|
238
|
+
const logger = loggerMain.createLogger(ScriptsAspect.id);
|
|
239
|
+
const scriptsService = new ScriptsService();
|
|
240
|
+
const scriptsMain = new ScriptsMain(workspace, envs, logger, componentAspect, config);
|
|
241
|
+
|
|
242
|
+
// Register service with envs
|
|
243
|
+
envs.registerService(scriptsService);
|
|
244
|
+
|
|
245
|
+
// Register CLI command
|
|
246
|
+
const scriptCmd = new ScriptCmd(scriptsMain);
|
|
247
|
+
cli.register(scriptCmd);
|
|
248
|
+
|
|
249
|
+
return scriptsMain;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
ScriptsAspect.addRuntime(ScriptsMain);
|
|
254
|
+
|
|
255
|
+
export default ScriptsMain;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { EnvService, EnvDefinition, Env, EnvContext, ServiceTransformationMap } from '@teambit/envs';
|
|
2
|
+
import chalk from 'chalk';
|
|
3
|
+
import type { Scripts } from './scripts';
|
|
4
|
+
import type { ScriptsMap } from './script-definition';
|
|
5
|
+
|
|
6
|
+
export type ScriptsDescriptor = {
|
|
7
|
+
id: string;
|
|
8
|
+
displayName: string;
|
|
9
|
+
scripts: ScriptsMap;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
type ScriptsTransformationMap = ServiceTransformationMap & {
|
|
13
|
+
getScripts: () => Scripts;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export class ScriptsService implements EnvService<{}, ScriptsDescriptor> {
|
|
17
|
+
name = 'Scripts';
|
|
18
|
+
|
|
19
|
+
async render(env: EnvDefinition) {
|
|
20
|
+
const descriptor = await this.getDescriptor(env);
|
|
21
|
+
if (!descriptor || Object.keys(descriptor.scripts).length === 0) {
|
|
22
|
+
return '';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const title = chalk.green('available scripts:');
|
|
26
|
+
const scriptsList = Object.entries(descriptor.scripts)
|
|
27
|
+
.map(([name, handler]) => {
|
|
28
|
+
const handlerStr = typeof handler === 'function' ? chalk.gray('[function]') : chalk.cyan(handler);
|
|
29
|
+
return ` ${chalk.bold(name)}: ${handlerStr}`;
|
|
30
|
+
})
|
|
31
|
+
.join('\n');
|
|
32
|
+
|
|
33
|
+
return `${title}\n${scriptsList}`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async getDescriptor(env: EnvDefinition): Promise<ScriptsDescriptor | undefined> {
|
|
37
|
+
if (!env.env.getScripts) return undefined;
|
|
38
|
+
const scripts = env.env.getScripts();
|
|
39
|
+
if (!scripts) return undefined;
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
id: this.name,
|
|
43
|
+
displayName: this.name,
|
|
44
|
+
scripts: scripts.getAll(),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
transform(env: Env, context: EnvContext): ScriptsTransformationMap | undefined {
|
|
49
|
+
if (!env?.scripts) return undefined;
|
|
50
|
+
return {
|
|
51
|
+
getScripts: () => env.scripts()(context),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
}
|
package/scripts.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { EnvContext, EnvHandler } from '@teambit/envs';
|
|
2
|
+
import type { ScriptHandler, ScriptsMap } from './script-definition';
|
|
3
|
+
|
|
4
|
+
export class Scripts {
|
|
5
|
+
constructor(private scriptsMap: ScriptsMap) {}
|
|
6
|
+
|
|
7
|
+
static from(scripts: ScriptsMap): EnvHandler<Scripts> {
|
|
8
|
+
return (_context: EnvContext) => {
|
|
9
|
+
return new Scripts(scripts);
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
get(name: string): ScriptHandler | undefined {
|
|
14
|
+
return this.scriptsMap[name];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
has(name: string): boolean {
|
|
18
|
+
return name in this.scriptsMap;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
list(): string[] {
|
|
22
|
+
return Object.keys(this.scriptsMap);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
getAll(): ScriptsMap {
|
|
26
|
+
return this.scriptsMap;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
isEmpty(): boolean {
|
|
30
|
+
return this.list().length === 0;
|
|
31
|
+
}
|
|
32
|
+
}
|
package/types/asset.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
declare module '*.png' {
|
|
2
|
+
const value: any;
|
|
3
|
+
export = value;
|
|
4
|
+
}
|
|
5
|
+
declare module '*.svg' {
|
|
6
|
+
import type { FunctionComponent, SVGProps } from 'react';
|
|
7
|
+
|
|
8
|
+
export const ReactComponent: FunctionComponent<SVGProps<SVGSVGElement> & { title?: string }>;
|
|
9
|
+
const src: string;
|
|
10
|
+
export default src;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// @TODO Gilad
|
|
14
|
+
declare module '*.jpg' {
|
|
15
|
+
const value: any;
|
|
16
|
+
export = value;
|
|
17
|
+
}
|
|
18
|
+
declare module '*.jpeg' {
|
|
19
|
+
const value: any;
|
|
20
|
+
export = value;
|
|
21
|
+
}
|
|
22
|
+
declare module '*.gif' {
|
|
23
|
+
const value: any;
|
|
24
|
+
export = value;
|
|
25
|
+
}
|
|
26
|
+
declare module '*.bmp' {
|
|
27
|
+
const value: any;
|
|
28
|
+
export = value;
|
|
29
|
+
}
|