1 | 'use strict';
|
2 |
|
3 | const path = require('path');
|
4 |
|
5 | const precinct = require('precinct');
|
6 | const resolve = require('resolve');
|
7 | const resolvePkg = require('resolve-pkg');
|
8 | const requirePackageName = require('require-package-name');
|
9 | const glob = require('glob');
|
10 |
|
11 | module.exports = function(filename, serverless) {
|
12 | const base = path.dirname(filename);
|
13 |
|
14 | const filePaths = new Set();
|
15 | const modulePaths = new Set();
|
16 |
|
17 | const localFilesToProcess = [filename];
|
18 |
|
19 | while (localFilesToProcess.length) {
|
20 | const currentLocalFile = localFilesToProcess.pop();
|
21 |
|
22 | if (filePaths.has(currentLocalFile)) {
|
23 | continue;
|
24 | }
|
25 |
|
26 | filePaths.add(currentLocalFile);
|
27 |
|
28 | precinct.paperwork(currentLocalFile).forEach(name => {
|
29 | if (resolve.isCore(name)) {
|
30 | return;
|
31 | }
|
32 |
|
33 | if (name.indexOf('.') === 0) {
|
34 | const abs = resolve.sync(name, {
|
35 | basedir: path.dirname(currentLocalFile)
|
36 | });
|
37 | localFilesToProcess.push(abs);
|
38 | } else {
|
39 | const moduleName = requirePackageName(name.replace(/\\/, '/'));
|
40 | const pathToModule = resolvePkg(moduleName, {
|
41 | cwd: base
|
42 | });
|
43 |
|
44 | if (path) {
|
45 | modulePaths.add(pathToModule);
|
46 | } else {
|
47 | throw new Error(`[serverless-plugin-include-dependencies]: Could not find ${name}`);
|
48 | }
|
49 | }
|
50 | });
|
51 | }
|
52 |
|
53 | const modulePathsToProcess = Array.from(modulePaths);
|
54 |
|
55 | modulePaths.clear();
|
56 |
|
57 | while (modulePathsToProcess.length) {
|
58 | const currentModulePath = modulePathsToProcess.pop();
|
59 |
|
60 | if (modulePaths.has(currentModulePath)) {
|
61 | continue;
|
62 | }
|
63 |
|
64 | modulePaths.add(currentModulePath);
|
65 |
|
66 | const packageJson = require(path.join(currentModulePath, 'package.json'));
|
67 |
|
68 | if (packageJson.dependencies) {
|
69 | Object.keys(packageJson.dependencies).forEach(dependency => {
|
70 | const pathToModule = resolvePkg(dependency, {
|
71 | cwd: currentModulePath
|
72 | });
|
73 |
|
74 | if (pathToModule) {
|
75 | modulePathsToProcess.push(pathToModule);
|
76 | } else {
|
77 | if (packageJson.optionalDependencies && dependency in packageJson.optionalDependencies) {
|
78 | return;
|
79 | }
|
80 | throw new Error(`[serverless-plugin-include-dependencies]: Could not find ${dependency}`);
|
81 | }
|
82 | });
|
83 | }
|
84 |
|
85 | if (packageJson.optionalDependencies) {
|
86 | Object.keys(packageJson.optionalDependencies).forEach(dependency => {
|
87 | const pathToModule = resolvePkg(dependency, {
|
88 | cwd: currentModulePath
|
89 | });
|
90 |
|
91 | if (pathToModule) {
|
92 | modulePathsToProcess.push(pathToModule);
|
93 | } else {
|
94 | serverless.cli.log(`[serverless-plugin-include-dependencies]: missing optional dependency: ${dependency}`);
|
95 | }
|
96 | });
|
97 | }
|
98 | }
|
99 |
|
100 | modulePaths.forEach(modulePath => {
|
101 | const moduleFilePaths = glob.sync(path.join(modulePath, '**'), {
|
102 | nodir: true,
|
103 | ignore: path.join(modulePath, 'node_modules', '**'),
|
104 | absolute: true
|
105 | });
|
106 |
|
107 | moduleFilePaths.forEach(moduleFilePath => {
|
108 | filePaths.add(moduleFilePath);
|
109 | });
|
110 | });
|
111 |
|
112 | return Array.from(filePaths);
|
113 | };
|