Skip to content

Commit a9a3125

Browse files
author
Murat
committed
feat: added script support
1 parent 48f5ca0 commit a9a3125

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1548
-306
lines changed

src/__tests__/unit/tasks/appDelegateTask.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ describe('appDelegateTask', () => {
194194
prepend: '#import <Firebase.h>',
195195
},
196196
{
197-
when: { test: 'random' },
197+
when: 'test === "random"',
198198
block: 'didFinishLaunchingWithOptions',
199199
prepend: '[FIRApp configure];',
200200
},
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { scriptTask } from '../../../tasks/scriptTask';
2+
import { ScriptTaskType } from '../../../types/mod.types';
3+
import { variables } from '../../../variables';
4+
5+
describe('scriptTask', () => {
6+
it('should work', async () => {
7+
const task: ScriptTaskType = {
8+
task: 'script',
9+
actions: [
10+
{
11+
script: `
12+
set('script', 'working')
13+
`,
14+
},
15+
],
16+
};
17+
18+
await scriptTask({
19+
configPath: 'path/to/config',
20+
task: task,
21+
packageName: 'test-package',
22+
});
23+
24+
expect(variables.get('script')).toEqual('working');
25+
});
26+
});

src/__tests__/unit/utils/applyContentModification.spec.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
require('../../mocks/mockAll');
2+
13
import { findOrCreateBlock } from '../../../tasks/buildGradleTask';
24
import { applyContentModification } from '../../../utils/applyContentModification';
35

4-
require('../../mocks/mockAll');
5-
66
describe('applyContentModification', () => {
77
it('should replace without search correctly', async () => {
88
let content = `
@@ -30,6 +30,36 @@ buildscript {
3030
google();
3131
}
3232
}
33+
`);
34+
});
35+
it('should replace using script correctly', async () => {
36+
let content = `
37+
buildscript {
38+
ext {
39+
jcenter();
40+
jcenter();
41+
}
42+
}
43+
`;
44+
content = await applyContentModification({
45+
action: {
46+
block: 'buildscript.ext',
47+
script: `
48+
await replace("google();")
49+
`,
50+
},
51+
findOrCreateBlock,
52+
configPath: 'path/to/config',
53+
packageName: 'package-name',
54+
content,
55+
indentation: 4,
56+
});
57+
expect(content).toMatch(`
58+
buildscript {
59+
ext {
60+
google();
61+
}
62+
}
3363
`);
3464
});
3565
});

src/__tests__/unit/utils/upgrade/runUpgradeTasks.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ env:
4646
value: true
4747
steps:
4848
- task: app_delegate
49-
when:
50-
value: false
49+
when: '!value'
5150
actions:
5251
- append: test`
5352
);

src/__tests__/unit/variables.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,19 @@ describe('variables', () => {
8484
expect(replaced).toEqual('this is test');
8585
});
8686
it('should replace with description for non existing variables', () => {
87-
const replaced = getText('this is $[test:description]');
87+
const replaced = getText(
88+
'this is $[get("nonExisting") ?? "description"]'
89+
);
8890
expect(replaced).toEqual('this is description');
8991
});
92+
it('should process script', () => {
93+
const replaced = getText(`this is $[
94+
set("test", false);
95+
test = true;
96+
]`);
97+
expect(replaced).toEqual('this is true');
98+
expect(variables.get('test')).toEqual(true);
99+
});
90100
});
91101
describe('transformTextInObject', () => {
92102
it('should return null', () => {

src/integrate.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import {
1313
import { AnalyzedPackages, LockProjectData } from './types/integrator.types';
1414
import { IntegrationConfig, PackageWithConfig } from './types/mod.types';
1515
import { analyzePackages } from './utils/analyzePackages';
16+
import { checkCondition } from './utils/checkCondition';
1617
import { getErrMessage } from './utils/getErrMessage';
1718
import { getPackageConfig } from './utils/getPackageConfig';
1819
import { logInfoNote } from './utils/logInfoNote';
1920
import { parseConfig } from './utils/parseConfig';
2021
import { runTask } from './utils/runTask';
21-
import { satisfies } from './utils/satisfies';
2222
import { setState } from './utils/setState';
2323
import { taskManager } from './utils/taskManager';
2424
import { topologicalSort } from './utils/topologicalSort';
@@ -260,10 +260,7 @@ export async function integrate(packageName?: string): Promise<void> {
260260
await logInfoNote(config.preInfo);
261261

262262
for (const task of config.steps) {
263-
if (
264-
task.when &&
265-
!satisfies(variables.getStore(), transformTextInObject(task.when))
266-
) {
263+
if (task.when && !checkCondition(task.when)) {
267264
setState(task.name, {
268265
state: 'skipped',
269266
error: false,

src/processScript.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/* eslint-disable @typescript-eslint/no-unused-vars,@typescript-eslint/no-implied-eval,no-shadow-restricted-names,@typescript-eslint/no-unsafe-call */
2+
import type { variables } from './variables';
3+
4+
const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor;
5+
6+
export function processScript(
7+
script: string,
8+
_variables: typeof variables,
9+
inline: boolean,
10+
async: true,
11+
ctx?: any
12+
): Promise<any>;
13+
export function processScript(
14+
script: string,
15+
_variables: typeof variables,
16+
inline: boolean,
17+
async: false,
18+
ctx?: any
19+
): any;
20+
export function processScript(
21+
script: string,
22+
_variables: typeof variables,
23+
inline: boolean,
24+
async: boolean,
25+
ctx?: any
26+
): any {
27+
const randomEvalName = 'eval_' + Math.random().toString().substring(2);
28+
const functionSet = {
29+
get: function (variable: string): any {
30+
return _variables.get(variable);
31+
},
32+
set: function (variable: string, value: any): any {
33+
if (!_variables.has(variable))
34+
Object.defineProperty(scope, variable, {
35+
get(): any {
36+
return _variables.get(variable);
37+
},
38+
set(value): any {
39+
return _variables.set(variable, value);
40+
},
41+
});
42+
43+
_variables.set(variable, value);
44+
45+
return '';
46+
},
47+
json: function (object: any): string {
48+
return JSON.stringify(object, null, 2);
49+
},
50+
parse: function (str: string): any {
51+
return JSON.parse(str);
52+
},
53+
require: function () {
54+
throw new Error('require is not allowed');
55+
},
56+
Function: function () {
57+
throw new Error('Function is not allowed');
58+
},
59+
__import_not_allowed: function () {
60+
throw new Error('import is not allowed');
61+
},
62+
globalThis: undefined,
63+
global: undefined,
64+
};
65+
const scope = Object.assign(
66+
Object.keys(globalThis).reduce(
67+
(acc, k) => {
68+
acc[k] = undefined;
69+
return acc;
70+
},
71+
{} as Record<string, any>
72+
),
73+
functionSet,
74+
ctx
75+
);
76+
77+
for (const storeKey in _variables.getStore()) {
78+
Object.defineProperty(scope, randomEvalName, {
79+
value: eval,
80+
});
81+
Object.defineProperty(scope, storeKey, {
82+
get(): any {
83+
return _variables.get(storeKey);
84+
},
85+
set(value): any {
86+
return _variables.set(storeKey, value);
87+
},
88+
});
89+
}
90+
script = script.replace(/\bimport\(/g, '__import_not_allowed(');
91+
script = 'var eval=undefined;' + script;
92+
93+
if (async) {
94+
if (inline) {
95+
return new AsyncFunction(
96+
'script',
97+
'with(this) { return eval(script) }'
98+
).call(scope, script);
99+
} else {
100+
return new AsyncFunction('with(this) { ' + script + ' }').call(scope);
101+
}
102+
}
103+
104+
if (inline) {
105+
return new Function('script', 'with(this) { return eval(script) }').call(
106+
scope,
107+
script
108+
);
109+
} else {
110+
return new Function('with(this) { ' + script + ' }').call(scope);
111+
}
112+
}

0 commit comments

Comments
 (0)