-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
executable file
·292 lines (251 loc) · 10.1 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/env node
/* eslint-disable import/no-named-as-default */
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/restrict-template-expressions */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import AdmZip from 'adm-zip';
import fs from 'fs';
import path from 'path';
import { createLogger, format, transports } from 'winston';
import { hideBin } from 'yargs/helpers';
import yargs from 'yargs/yargs';
let glob;
const importedGlob = await import('glob');
glob = importedGlob.default || importedGlob;
const argv = yargs(hideBin(process.argv))
.option('verbose', {
alias: 'v',
type: 'boolean',
description: 'Run with verbose logging',
})
.option('calcite', {
alias: 'C',
type: 'boolean',
description: 'Include calcite assets',
})
.option('config', {
alias: 'c',
type: 'string',
description: 'Path to configuration file',
requiresArg: true,
})
.option('package-manager', {
alias: 'p',
type: 'string',
description: 'Specify package manager to use',
choices: ['npm', 'yarn', 'pnpm'],
default: 'npm',
requiresArg: true,
}).argv;
const loggingLevel = argv.verbose ? 'verbose' : 'info';
const { combine, timestamp: timestampf, label: labelf, printf, colorize } = format;
const loggerFormat = printf(({ level, message, label, timestamp }) => {
return `${timestamp} [${label}] ${level}: ${message}`;
});
const logger = createLogger({
level: loggingLevel,
defaultMeta: { service: 'arcgis-next-copy-assets' },
format: combine(labelf({ label: 'arcgis-next-copy-assets' }), colorize(), timestampf(), loggerFormat),
transports: [new transports.Console()],
});
logger.debug(`Argv: ${JSON.stringify(argv, null, 2)}`);
const mkDirIfNotExists = dir => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
} else {
logger.verbose(`Directory ${dir} already exists.`);
}
};
const copyDirectoryRecursive = (source, destination) => {
mkDirIfNotExists(destination);
const files = fs.readdirSync(source);
files.forEach(file => {
const sourceFilePath = path.join(source, file);
const destinationFilePath = path.join(destination, file);
if (fs.statSync(sourceFilePath).isDirectory()) {
copyDirectoryRecursive(sourceFilePath, destinationFilePath);
} else {
fs.copyFileSync(sourceFilePath, destinationFilePath);
}
});
};
const deleteDirectoryRecursive = directory => {
if (fs.existsSync(directory)) {
const files = fs.readdirSync(directory);
files.forEach(file => {
const filePath = path.join(directory, file);
if (fs.statSync(filePath).isDirectory()) {
deleteDirectoryRecursive(filePath);
} else {
fs.unlinkSync(filePath);
}
});
fs.rmdirSync(directory);
}
};
const checkDependencyExists = (packageJson, dependency) => {
const dependencies = packageJson && packageJson.dependencies ? packageJson.dependencies : {};
const peerDependencies = packageJson && packageJson.peerDependencies ? packageJson.peerDependencies : {};
if (dependency in dependencies || dependency in peerDependencies) {
logger.verbose(`Found dependency ${dependency} in package.json`);
} else {
logger.error(`Dependency ${dependency} not found in in package.json`);
process.exit(1);
}
};
const extractZippedDependency = (cache, zipPattern, targetDirectory) => {
logger.verbose(`Searching for zip file with pattern ${zipPattern} in ${cache}...`);
const zipFiles = glob.sync(zipPattern, { cwd: cache }); // find zip file in cache directory
if (zipFiles.length === 0) {
// handle case where no zip file is found
logger.error(`No zip file found for: ${zipPattern}`);
process.exit(1);
}
const parsedVersions = zipFiles.map(parseVersion).filter(v => v !== null);
logger.debug(`Parsed versions:\n${JSON.stringify(parsedVersions, null, 2)}`);
const sortedParsedVersions = parsedVersions.sort(compareVersions);
logger.debug(`Sorted parsed versions:\n${JSON.stringify(sortedParsedVersions, null, 2)}`);
const sortedZipFiles = sortedParsedVersions.map(item => item.originalString);
logger.verbose(`Found ${sortedZipFiles.length} zip files.\n${JSON.stringify(sortedZipFiles, null, 2)}`);
const zipFile = sortedZipFiles[0];
logger.verbose(`Using zip file: ${zipFile}`);
const zipFilePath = path.join('../../.yarn/cache', zipFile);
const zip = new AdmZip(zipFilePath);
zip.extractAllTo(targetDirectory, true);
logger.verbose("Extracted asset's zip file successfully.");
};
// Function to extract and parse version number from string
const parseVersion = (versionString) => {
const versionMatch = versionString.match(/(\d+\.\d+\.\d+)/);
if (versionMatch) {
return {
version: versionMatch[0].split('.').map(Number),
originalString: versionString
};
}
return null;
};
// Custom comparator for version numbers
const compareVersions = (a, b) => {
if (!a || !b || !a.version || !b.version) {
return 0;
}
for (let i = 0; i < Math.max(a.version.length, b.version.length); i++) {
const aVersionPart = i < a.version.length ? a.version[i] : 0;
const bVersionPart = i < b.version.length ? b.version[i] : 0;
if (aVersionPart !== bVersionPart) {
return bVersionPart - aVersionPart; // For descending order
}
}
return 0;
};
const main = config => {
logger.info('Starting arcgis-next-copy-assets...');
logger.debug(`(Main) Configuration: ${JSON.stringify(config, null, 2)}`);
const cwd = process.cwd();
logger.verbose(`Current working directory: ${cwd}`);
const isPnpm = config.packageManager === 'pnpm';
const includeCalcite = config.calcite ? config.calcite : false;
const publicDirectory =
config && config.publicDirectory ? path.join(cwd, config.publicDirectory) : path.join(cwd, 'public'); // public directory to copy assets to
const targetDirectory = path.join(publicDirectory, 'unzipped-dependency'); // temp directory to unzip the dependency to
logger.verbose(`Creating working directory: ${targetDirectory}`);
mkDirIfNotExists(targetDirectory);
const packageJsonPath =
config && config.packageJsonPath ? path.join(cwd, config.packageJsonPath) : path.join(cwd, 'package.json');
const packageJsonContents = fs.readFileSync(packageJsonPath, 'utf-8');
logger.verbose(`Reading package.json file at ${packageJsonPath}...`);
const packageJson = JSON.parse(packageJsonContents);
const baseAssetsDirectory = isPnpm ? path.join(targetDirectory) : path.join(cwd);
const arcgisAssetsDirectory = path.join(baseAssetsDirectory, 'node_modules/@arcgis/core/assets');
const calciteAssetsDirectory = path.join(
baseAssetsDirectory,
'node_modules/@esri/calcite-components/dist/calcite/assets',
);
const packages = [
{
name: '@arcgis/core',
pattern: '@arcgis-core-npm-*.zip',
assetsDirectory: arcgisAssetsDirectory,
outputDirectory: path.join(publicDirectory, 'arcgis'),
},
];
if (includeCalcite) {
packages.push({
name: '@esri/calcite-components',
pattern: '@esri-calcite-components-npm-*.zip',
assetsDirectory: calciteAssetsDirectory,
outputDirectory: path.join(publicDirectory, 'calcite'),
});
}
packages.forEach(({ name, outputDirectory }) => {
checkDependencyExists(packageJson, name);
logger.verbose(`Deleting existing assets directory: ${outputDirectory}`);
deleteDirectoryRecursive(outputDirectory);
logger.verbose(`Creating assets directory: ${outputDirectory}`);
mkDirIfNotExists(outputDirectory);
});
if (isPnpm) {
logger.verbose('Extracting assets from pnpm cache directory to working directory...');
const cacheDirectory =
config && config.cacheDirectory ? path.join(cwd, config.cacheDirectory) : path.join(cwd, '../..', '.yarn/cache'); // cache directory in a pnpm/yarn workspace
packages.forEach(({ name, pattern }) => {
logger.verbose(`Extracting ${name} zip file...`);
extractZippedDependency(cacheDirectory, pattern, targetDirectory);
});
}
packages.forEach(({ assetsDirectory, outputDirectory }) => {
if (fs.existsSync(assetsDirectory)) {
logger.verbose(`Copying assets from ${assetsDirectory} to ${outputDirectory}`);
copyDirectoryRecursive(assetsDirectory, outputDirectory);
} else {
logger.error(`Could not find assets directory: ${assetsDirectory}`);
process.exit(1);
}
});
logger.verbose(`Deleting temporary working directory: ${targetDirectory}`);
deleteDirectoryRecursive(targetDirectory);
logger.info('Completed copying @argis/core assets to nextjs public directory');
};
const parseConfig = async configPath => {
let config = {};
if (configPath) {
const configFile = path.join(process.cwd(), configPath);
if (fs.existsSync(configFile)) {
logger.verbose(`Loading configuration from ${configFile}`);
const extname = path.extname(configFile).toLowerCase();
if (['.json'].includes(extname)) {
const configContent = fs.readFileSync(configFile, 'utf8');
config = JSON.parse(configContent);
} else if (['.js', '.mjs'].includes(extname)) {
try {
const configModule = await import(configFile);
config = configModule.default || configModule;
} catch (err) {
logger.error(`Error loading JavaScript/TypeScript module: ${err}`);
}
} else {
logger.error(`Unsupported configuration file format: ${extname}`);
process.exit(1);
}
logger.debug(`Configuration: ${JSON.stringify(config, null, 2)}`);
} else {
logger.warn(`Configuration file ${configFile} does not exist`);
}
} else {
logger.warn(`No configuration file specified`);
}
return config;
};
const parsedConfig = await parseConfig(argv.config);
let packageManager;
if (argv['package-manager']) {
packageManager = argv['package-manager'];
logger.verbose(`Using package manager: ${packageManager}`);
}
const calcite = argv.calcite ? true : false;
logger.verbose(`Including calcite assets: ${calcite}`);
main({ ...parsedConfig, packageManager, calcite });