Skip to content

Commit e173080

Browse files
committed
Set correct override permissions for non-root usage in Docker
1 parent 08020b0 commit e173080

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

pack.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const packageApp = async () => {
3636
await Promise.all([
3737
// Include the packaging & build scripts:
3838
'build-release.sh',
39+
'prepare.ts',
3940
// Include package-lock.json, to keep dependencies locked:
4041
'package-lock.json',
4142
// Add the fully bundled source (not normally packaged by npm):

package-lock.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"start": "node-dev ./bin/run start",
1818
"build:src": "rm -rf lib && tsc -b --force",
1919
"build:release": "oclif-dev manifest && webpack && ts-node ./pack.ts",
20-
"prepare": "cd ./overrides/js && npm install",
20+
"prepare": "ts-node ./prepare.ts",
2121
"prepack": "npm run build:src && oclif-dev manifest",
2222
"test": "cross-env TS_NODE_FILES=true mocha --exit -r ts-node/register 'test/**/*.spec.ts'",
2323
"test:release": "cross-env TEST_BUILT_TARBALL=1 npm run test"
@@ -87,6 +87,7 @@
8787
"@types/env-paths": "^1.0.2",
8888
"@types/express": "^4.16.1",
8989
"@types/fs-extra": "^8.0.0",
90+
"@types/klaw": "^3.0.2",
9091
"@types/lodash": "^4.14.117",
9192
"@types/mocha": "^5.2.5",
9293
"@types/node": "^16.3.2",
@@ -102,6 +103,7 @@
102103
"fs-extra": "^8.1.0",
103104
"got": "^9.6.0",
104105
"graphql.js": "^0.6.1",
106+
"klaw": "^4.0.1",
105107
"mocha": "^8.2.1",
106108
"needle": "^2.4.0",
107109
"node-dev": "^6.4.0",

prepare.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import * as path from 'path';
2+
import type { Stats } from 'fs';
3+
import * as fs from 'fs/promises';
4+
import * as klaw from 'klaw';
5+
import { spawn as spawnAsync, SpawnOptions } from 'child_process';
6+
7+
const spawn = (command: string, args: string[] = [], options: SpawnOptions = {}) => {
8+
return new Promise<void>((resolve, reject) => {
9+
const proc = spawnAsync(command, args, options);
10+
proc.on('exit', (code) => {
11+
if (code === 0) resolve();
12+
else reject(new Error(
13+
`Spawn ${command} ${args.join(' ')} exited with ${code}`
14+
));
15+
});
16+
});
17+
}
18+
19+
const collectAsyncIterator = async (asyncIterator: any) => {
20+
const result = [];
21+
for await (const value of asyncIterator) result.push(value);
22+
return result;
23+
}
24+
25+
26+
const OVERRIDES_DIR = path.join(__dirname, 'overrides');
27+
28+
(async () => {
29+
console.log('Installing override npm dependencies...');
30+
31+
await spawn('npm', ['install', '--production'], {
32+
cwd: path.join(OVERRIDES_DIR, 'js'),
33+
stdio: 'inherit'
34+
});
35+
36+
const files: Array<{
37+
path: string,
38+
stats: Stats
39+
}> = await collectAsyncIterator(klaw(OVERRIDES_DIR));
40+
41+
// For Docker we don't know the user in the container, so all override files must
42+
// be globally readable (and directories globally executable)
43+
await files.map(({ path, stats }) =>
44+
stats.isDirectory()
45+
? fs.chmod(path, stats.mode | 0o5) // Set o+rx
46+
: fs.chmod(path, stats.mode | 0o4) // Set o+r
47+
);
48+
49+
console.log('Override dependencies installed');
50+
})();

0 commit comments

Comments
 (0)