Skip to content

Commit

Permalink
feat(node)!: require prepare, drop prefix config, redirect home
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice committed Jul 24, 2024
1 parent 6ac0f74 commit 770d838
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 53 deletions.
2 changes: 2 additions & 0 deletions src/cli/prepare-tool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
PrepareJavaJreService,
PrepareJavaService,
} from '../tools/java';
import { PrepareNodeService } from '../tools/node';

Check warning on line 12 in src/cli/prepare-tool/index.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/prepare-tool/index.ts#L12

Added line #L12 was not covered by tests
import { logger } from '../utils';
import { PrepareLegacyToolsService } from './prepare-legacy-tools.service';
import { PREPARE_TOOL_TOKEN, PrepareToolService } from './prepare-tool.service';
Expand All @@ -30,6 +31,7 @@ function prepareContainer(): Container {
container.bind(PREPARE_TOOL_TOKEN).to(PrepareJavaService);
container.bind(PREPARE_TOOL_TOKEN).to(PrepareJavaJreService);
container.bind(PREPARE_TOOL_TOKEN).to(PrepareJavaJdkService);
container.bind(PREPARE_TOOL_TOKEN).to(PrepareNodeService);

Check warning on line 34 in src/cli/prepare-tool/index.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/prepare-tool/index.ts#L34

Added line #L34 was not covered by tests

logger.trace('preparing container done');
return container;
Expand Down
1 change: 0 additions & 1 deletion src/cli/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export const NoPrepareTools = [
'gradle',
'lerna',
'maven',
'node',
'npm',
'pnpm',
'renovate',
Expand Down
83 changes: 32 additions & 51 deletions src/cli/tools/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from 'node:fs/promises';
import { join } from 'node:path';
import { env as penv } from 'node:process';
import { codeBlock } from 'common-tags';
import { execa } from 'execa';
import { inject, injectable } from 'inversify';
import { PrepareToolBaseService } from '../../prepare-tool/prepare-tool-base.service';

Check warning on line 6 in src/cli/tools/node/index.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/tools/node/index.ts#L6

Added line #L6 was not covered by tests
import {
CompressionService,
EnvService,
Expand All @@ -14,15 +14,32 @@ import {
import { getDistro, parse } from '../../utils';
import {
InstallNodeBaseService,
prepareGlobalConfig,
prepareUserConfig,
prepareNpmCache,
prepareNpmrc,
prepareSymlinks,

Check warning on line 19 in src/cli/tools/node/index.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/tools/node/index.ts#L17-L19

Added lines #L17 - L19 were not covered by tests
} from './utils';

@injectable()
export class PrepareNodeService extends PrepareToolBaseService {
override name = 'node';
override async execute(): Promise<void> {
await prepareNpmCache(this.pathSvc);
await prepareNpmrc(this.pathSvc);
await prepareSymlinks(this.envSvc, this.pathSvc);

await this.pathSvc.exportToolEnv(this.name, {
NO_UPDATE_NOTIFIER: '1',
npm_config_update_notifier: 'false',
npm_config_fund: 'false',
});
}
}

Check warning on line 37 in src/cli/tools/node/index.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/tools/node/index.ts#L22-L37

Added lines #L22 - L37 were not covered by tests
@injectable()
export class InstallNodeService extends InstallNodeBaseService {
readonly name = 'node';

private get arch(): string {
private get nodeArch(): string {

Check warning on line 42 in src/cli/tools/node/index.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/tools/node/index.ts#L42

Added line #L42 was not covered by tests
switch (this.envSvc.arch) {
case 'arm64':
return 'arm64';
Expand Down Expand Up @@ -51,36 +68,29 @@ export class InstallNodeService extends InstallNodeBaseService {
}

override async install(version: string): Promise<void> {
const distro = await getDistro();
const arch = this.arch;
const name = this.name;
const versionCode = distro.versionCode;
let filename = `${version}/${name}-${version}-${this.ghArch}.tar.xz`;
let checksumFileUrl = `https://github.com/containerbase/${name}-prebuild/releases/download/${filename}.sha512`;
let isOnGithub = await this.http.exists(checksumFileUrl);
let file: string;

if (isOnGithub) {
// no distro specific prebuilds
const checksumFile = await this.http.download({ url: checksumFileUrl });
const expectedChecksum = (
await fs.readFile(checksumFile, 'utf-8')
).trim();
const expectedChecksum = await this.getChecksum(checksumFileUrl);

Check warning on line 79 in src/cli/tools/node/index.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/tools/node/index.ts#L79

Added line #L79 was not covered by tests
file = await this.http.download({
url: `https://github.com/containerbase/${name}-prebuild/releases/download/${filename}`,
checksumType: 'sha512',
expectedChecksum,
});
} else {
const distro = await getDistro();
const versionCode = distro.versionCode;

Check warning on line 87 in src/cli/tools/node/index.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/tools/node/index.ts#L86-L87

Added lines #L86 - L87 were not covered by tests
// distro specific prebuilds
filename = ` ${version}/${name}-${version}-${versionCode}-${this.ghArch}.tar.xz`;
checksumFileUrl = `https://github.com/containerbase/${name}-prebuild/releases/download/${filename}.sha512`;
isOnGithub = await this.http.exists(checksumFileUrl);
if (isOnGithub) {
const checksumFile = await this.http.download({ url: checksumFileUrl });
const expectedChecksum = (
await fs.readFile(checksumFile, 'utf-8')
).trim();
const expectedChecksum = await this.getChecksum(checksumFileUrl);

Check warning on line 93 in src/cli/tools/node/index.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/tools/node/index.ts#L93

Added line #L93 was not covered by tests
file = await this.http.download({
url: `https://github.com/containerbase/${name}-prebuild/releases/download/${filename}`,
checksumType: 'sha512',
Expand All @@ -89,7 +99,7 @@ export class InstallNodeService extends InstallNodeBaseService {
} else {
// fallback to nodejs.org
checksumFileUrl = `https://nodejs.org/dist/v${version}/SHASUMS256.txt`;
filename = `${name}-v${version}-linux-${arch}.tar.xz`;
filename = `${name}-v${version}-linux-${this.nodeArch}.tar.xz`;

Check warning on line 102 in src/cli/tools/node/index.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/tools/node/index.ts#L102

Added line #L102 was not covered by tests
const checksumFile = await this.http.download({ url: checksumFileUrl });
const expectedChecksum = (await fs.readFile(checksumFile, 'utf-8'))
.split('\n')
Expand All @@ -108,23 +118,6 @@ export class InstallNodeService extends InstallNodeBaseService {
const path = await this.pathSvc.createVersionedToolPath(this.name, version);
await this.compress.extract({ file, cwd: path, strip: 1 });

if (this.envSvc.isRoot) {
await prepareGlobalConfig({
prefix: '/usr/local',
versionedToolPath: path,
});
await prepareUserConfig({
prefix: `${this.envSvc.userHome}/.npm-global`,
home: this.envSvc.userHome,
name: this.envSvc.userName,
});
} else {
await prepareGlobalConfig({
prefix: `${this.envSvc.userHome}/.npm-global`,
versionedToolPath: path,
});
}

const ver = parse(version);
if (ver.major < 15) {
const tmp = await fs.mkdtemp(
Expand All @@ -142,24 +135,6 @@ export class InstallNodeService extends InstallNodeBaseService {
force: true,
});
}

if (!(await this.pathSvc.toolEnvExists(this.name))) {
await this.pathSvc.exportToolEnv(this.name, {
NO_UPDATE_NOTIFIER: '1',
npm_config_update_notifier: 'false',
npm_config_fund: 'false',
});

await this.pathSvc.exportToolEnvContent(
this.name,
codeBlock`
# openshift override unknown user home
if [ "\${EUID}" != 0 ] && [ "\${EUID}" != ${this.envSvc.userId} ]; then
export npm_config_prefix="${this.envSvc.userHome}/.npm-global"
fi
`,
);
}
}

override async link(version: string): Promise<void> {
Expand Down Expand Up @@ -194,4 +169,10 @@ export class InstallNodeService extends InstallNodeBaseService {
});
}
}

private async getChecksum(checksumFileUrl: string): Promise<string> {
const checksumFile = await this.http.download({ url: checksumFileUrl });
const expectedChecksum = (await fs.readFile(checksumFile, 'utf-8')).trim();
return expectedChecksum;
}

Check warning on line 177 in src/cli/tools/node/index.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/tools/node/index.ts#L172-L177

Added lines #L172 - L177 were not covered by tests
}
28 changes: 28 additions & 0 deletions src/cli/tools/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,31 @@ async function readPackageJson(path: string): Promise<PackageJson> {
const data = await readFile(path, { encoding: 'utf8' });
return JSON.parse(data);
}

export async function prepareNpmCache(pathSvc: PathService): Promise<void> {
const path = join(pathSvc.homePath, '.npm');
if (!(await pathExists(path, true))) {
await pathSvc.createDir(path);
}
}

export async function prepareNpmrc(pathSvc: PathService): Promise<void> {
const path = join(pathSvc.homePath, '.npmrc');
if (!(await pathExists(path, false))) {
await fs.writeFile(path, '');
}
}

export async function prepareSymlinks(
envSvc: EnvService,
pathSvc: PathService,
): Promise<void> {
await fs.symlink(
join(pathSvc.homePath, '.npm'),
join(envSvc.userHome, '.npm'),
);
await fs.symlink(
join(pathSvc.homePath, '.npmrc'),
join(envSvc.userHome, '.npmrc'),
);
}

Check warning on line 306 in src/cli/tools/node/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/tools/node/utils.ts#L279-L306

Added lines #L279 - L306 were not covered by tests
6 changes: 5 additions & 1 deletion test/node/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ RUN set -ex; cd a; pnpm i

FROM base AS testd

RUN prepare-tool node

USER 1000

# renovate: datasource=node
Expand Down Expand Up @@ -230,7 +232,7 @@ ARG CONTAINERBASE_DEBUG
RUN install-tool node v14.18.1
RUN install-tool node v14.18.2

RUN cat $USER_HOME/.npmrc | grep "prefix = \"$USER_HOME/.npm-global\""
RUN set -ex; ls -la $USER_HOME/; test -L $USER_HOME/.npmrc

RUN touch /.dummy

Expand Down Expand Up @@ -453,6 +455,8 @@ RUN set -ex; \
#--------------------------------------
FROM base AS testq

RUN prepare-tool node

# install latest version
RUN install-tool node
RUN install-tool yarn
Expand Down

0 comments on commit 770d838

Please sign in to comment.