Repository navigation
Expand file tree
/
Copy pathinstall.js
More file actions
126 lines (117 loc) · 3.78 KB
/
Copy pathinstall.js
File metadata and controls
126 lines (117 loc) · 3.78 KB
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
import fs from 'fs/promises';
import path from 'path';
import { execa } from 'execa';
/**
* Test if a lockfile is checked in.
*
* @param {string} dir - Project dir.
* @param {string} lockFile - Lock file name.
* @return {boolean} - Whether the lock file exists and is checked in.
*/
async function hasLockFile( dir, lockFile ) {
if ( ( await fs.access( dir + '/' + lockFile ).catch( () => false ) ) === false ) {
return false;
}
const { stdout } = await execa( 'git', [ 'ls-files', lockFile ], { cwd: dir } );
return !! stdout;
}
/**
* Test if a composer lockfile exists and is valid.
*
* @param {string} dir - Project dir.
* @return {boolean} - Whether the lock file exists and is valid.
*/
async function isComposerLockOk( dir ) {
if ( ( await fs.access( dir + '/composer.lock' ).catch( () => false ) ) === false ) {
return false;
}
try {
await execa(
'composer',
[ 'validate', '--check-lock', '--no-check-all', '--no-check-publish' ],
{ cwd: dir, stdout: 'ignore' }
);
return true;
} catch ( e ) {
if ( e.exitCode === 2 ) {
return false;
}
throw e;
}
}
/**
* Get the directory for a slug.
*
* @param {string} project - Project slug.
* @param {string} file - File within the project to find.
* @return {string} Path.
*/
export function projectDir( project, file = '.' ) {
return path.resolve( project === 'monorepo' ? '.' : `projects/${ project }`, file );
}
/**
* Test if `pnpm install` is needed for a project.
*
* @param {string} project - Project slug.
* @return {boolean} Whether `pnpm install` is needed.
*/
export async function needsPnpmInstall( project ) {
if ( project === 'monorepo' ) {
return true;
}
return (
( await fs.access( projectDir( project, 'package.json' ) ).catch( () => false ) ) !== false
);
}
/**
* Determine install command arguments.
*
* @param {string} project - Project slug.
* @param {string} pkgMgr - Package manager.
* @param {object} argv - Argv object.
* @param {boolean} argv.production - Whether this is a production install.
* @param {boolean} argv.useUncommittedComposerLock - Whether to use uncommitted composer.lock files when valid.
* @return {string[]} Args to pass to the package manager.
*/
export async function getInstallArgs( project, pkgMgr, argv ) {
const args = [];
// For composer, choose 'install' or 'update' depending on whether the lockfile is checked in.
// For pnpm, the lockfile is always checked in thanks to the workspace thing.
if ( pkgMgr === 'composer' ) {
const dir = projectDir( project );
if ( await hasLockFile( dir, 'composer.lock' ) ) {
args.push( 'install' );
} else if ( argv.useUncommittedComposerLock && ( await isComposerLockOk( dir ) ) ) {
args.push( 'install' );
} else {
args.push( 'update' );
}
if ( project.startsWith( 'plugins/' ) && argv.production ) {
args.push( '-o', '--no-dev', '--classmap-authoritative', '--prefer-dist' );
}
} else if ( pkgMgr === 'pnpm' ) {
args.push( 'install' );
} else {
throw new Error( `Unknown package manager ${ pkgMgr }` );
}
return args;
}
/**
* Determine install command arguments.
*
* @param {string} dir - Project directory.
* @param {object} argv - Argv object.
* @param {boolean} argv.useUncommittedComposerLock - Whether to use uncommitted composer.lock files when valid.
* @return {string[]} Args to pass to the package manager.
*/
export async function getComposerInstallArgsForDir( dir, argv ) {
const args = [];
if ( await hasLockFile( dir, 'composer.lock' ) ) {
args.push( 'install' );
} else if ( argv.useUncommittedComposerLock && ( await isComposerLockOk( dir ) ) ) {
args.push( 'install' );
} else {
args.push( 'update' );
}
return args;
}