Skip to content

Commit 97ff859

Browse files
author
Murat
committed
fix(script): support downloading module from public repo
1 parent f4ce691 commit 97ff859

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

src/__tests__/mocks/mockFetch.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
mockIntegrateWithMinVersionYml,
77
} from './mockIntegrateWithMinRNVersion';
88
import { mockIntegrateYml } from './mockIntegrateYml';
9+
import { mockTestPluginTemplate } from './mockTestPluginTemplate';
910

1011
// @ts-ignore
1112
global.fetch = jest.fn((url: string) =>
@@ -24,6 +25,8 @@ global.fetch = jest.fn((url: string) =>
2425
return Promise.resolve(mockIntegrateWithInvalidMinVersionYml);
2526
else if (url.endsWith('integrate.yml'))
2627
return Promise.resolve(mockIntegrateYml);
28+
else if (url.endsWith('mockTestPlugin.js'))
29+
return Promise.resolve(mockTestPluginTemplate);
2730
else if (
2831
url.endsWith('/rn-diff-purge/release/1.0.0/RnDiffApp/package.json')
2932
)

src/__tests__/mocks/mockFs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ function isDirectoryMatch(path: string, directory: string): boolean {
1818
}
1919

2020
export const mockFs = {
21-
existsSync: (path: string): boolean => {
21+
existsSync: jest.fn((path: string): boolean => {
2222
path = nodePath.resolve(path);
2323
return Object.keys(store).some(key => isDirectoryMatch(key, path));
24-
},
24+
}),
2525
renameSync: (from: string, to: string) => {
2626
from = nodePath.resolve(from);
2727
to = nodePath.resolve(to);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const mockTestPluginTemplate = `module.exports = function plugin(ctx){
2+
ctx.set('script', 'working')
3+
}
4+
`;

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
require('../../mocks/mockAll');
2+
3+
import path from 'path';
14
import { scriptTask } from '../../../tasks/scriptTask';
25
import { ScriptTaskType } from '../../../types/mod.types';
36
import { taskManager } from '../../../utils/taskManager';
47
import { variables } from '../../../variables';
8+
import { mockFs } from '../../mocks/mockFs';
59

610
describe('scriptTask', () => {
711
it('should work', async () => {
@@ -27,6 +31,10 @@ describe('scriptTask', () => {
2731
expect(variables.get('script')).toEqual('working');
2832
});
2933
it('should require plugin', async () => {
34+
mockFs.writeFileSync(
35+
path.join(__dirname, '../../mocks/mockTestPlugin.js'),
36+
'dummy'
37+
);
3038
const task: ScriptTaskType = {
3139
task: 'script',
3240
actions: [
@@ -43,6 +51,35 @@ describe('scriptTask', () => {
4351
taskManager,
4452
});
4553

54+
expect(variables.get('script')).toEqual('working');
55+
});
56+
it('should download non existing plugin', async () => {
57+
mockFs.writeFileSync(
58+
path.join(__dirname, '../../mocks/mockTestPlugin.js'),
59+
'dummy'
60+
);
61+
const task: ScriptTaskType = {
62+
task: 'script',
63+
actions: [
64+
{
65+
module: '../../src/__tests__/mocks/mockTestPlugin.js',
66+
},
67+
],
68+
};
69+
70+
mockFs.existsSync.mockImplementationOnce(() => false);
71+
72+
await scriptTask({
73+
configPath: 'path/to/config',
74+
task: task,
75+
packageName: 'test-package',
76+
taskManager,
77+
});
78+
79+
expect(fetch as jest.Mock).toHaveBeenCalledWith(
80+
expect.stringContaining('mockTestPlugin.js')
81+
);
82+
4683
expect(variables.get('script')).toEqual('working');
4784
});
4885
});

src/tasks/scriptTask.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import fs from 'fs';
12
import path from 'path';
3+
import { Constants } from '../constants';
4+
import { downloadFile, getRemotePath } from '../utils/getPackageConfig';
25
import { processScript } from '../utils/processScript';
36
import {
47
ModStep,
@@ -71,6 +74,25 @@ export async function scriptTask(args: {
7174
ctx
7275
);
7376
} else {
77+
const pluginPath = path.join(
78+
path.dirname(args.configPath),
79+
action.module
80+
);
81+
if (
82+
!fs.existsSync(pluginPath) &&
83+
args.packageName !== Constants.UPGRADE_CONFIG_FILE_NAME
84+
) {
85+
const remotePath =
86+
getRemotePath(args.packageName, Constants.REMOTE_REPO) +
87+
pluginPath.replace(path.join(args.configPath, '../'), '');
88+
89+
const localDir = path.join(pluginPath, '..');
90+
if (!fs.existsSync(localDir))
91+
fs.mkdirSync(localDir, { recursive: true });
92+
const success = await downloadFile(remotePath, pluginPath);
93+
if (!success) throw new Error(`File not found at ${pluginPath}`);
94+
}
95+
7496
// eslint-disable-next-line @typescript-eslint/no-require-imports
7597
const plugin = require(
7698
path.relative(

0 commit comments

Comments
 (0)