Skip to content

Commit

Permalink
fix: update deps, add more tests, working on getting registry from ya…
Browse files Browse the repository at this point in the history
…rnrc
  • Loading branch information
dmeents committed Feb 23, 2023
1 parent 37ee502 commit 80cad06
Show file tree
Hide file tree
Showing 117 changed files with 647 additions and 439 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
npmPublishAccess: public
npmPublishRegistry: https://registry.npmjs.org

plugins:
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
Expand Down
26 changes: 14 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,37 @@
},
"packageManager": "yarn@3.4.1",
"devDependencies": {
"@dmeents/maestro": "^1.6.35",
"@dmeents/maestro": "^1.6.42",
"@dmeents/semantic-release-yarn": "1.1.2",
"@semantic-release/commit-analyzer": "^9.0.2",
"@semantic-release/git": "^10.0.1",
"@semantic-release/github": "^8.0.7",
"@semantic-release/release-notes-generator": "^10.0.3",
"@types/eslint": "^8.4.10",
"@types/jest": "^29.2.5",
"@types/eslint": "^8.21.1",
"@types/jest": "^29.4.0",
"@types/js-yaml": "^4",
"@types/prettier": "^2.7.2",
"@types/semantic-release__error": "^3.0.0",
"@types/semver": "^7.3.13",
"@typescript-eslint/eslint-plugin": "^5.48.1",
"@typescript-eslint/parser": "^5.48.1",
"eslint": "^8.31.0",
"@typescript-eslint/eslint-plugin": "^5.53.0",
"@typescript-eslint/parser": "^5.53.0",
"eslint": "^8.34.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-jest": "^27.2.1",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^29.3.1",
"jest-environment-jsdom": "^29.3.1",
"jest": "^29.4.3",
"jest-environment-jsdom": "^29.4.3",
"jest-junit": "^15.0.0",
"prettier": "^2.8.2",
"semantic-release": "^20.0.2",
"prettier": "^2.8.4",
"semantic-release": "^20.1.0",
"tempy": "^1.0.1",
"ts-jest": "^29.0.3",
"typescript": "^4.9.4"
"ts-jest": "^29.0.5",
"typescript": "^4.9.5"
},
"dependencies": {
"@semantic-release/error": "^3.0.0",
"execa": "^5.1.1",
"js-yaml": "^4.1.0",
"read-pkg": "^5.2.0",
"semver": "^7.3.8"
}
Expand Down
26 changes: 20 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { error, ErrorTypes } from './utils/error';
import { getChannel, getNpmAuthIdent, getNpmToken, getPackage } from './utils';
import { Context, PrepareContext } from './types';
import { Yarn } from './utils/yarn';
import yaml from 'js-yaml';
import * as fs from 'fs';

let verified = false;
let prepared = false;
Expand All @@ -16,7 +18,15 @@ export async function verifyConditions(

ctx.logger.log(`read ${ctx.cwd}/package.json`);
const packageJson = await getPackage(ctx.cwd);
const registry = packageJson?.publishConfig?.registry as string;

ctx.logger.log(`read ${ctx.cwd}/yarnrc.yml`);

const yarnrc = yaml.load(
fs.readFileSync(`${ctx.cwd}/.yarnrc.yml`, 'utf8'),
) as Record<string, string>;

const registryFromPackage = packageJson?.publishConfig?.registry as string;
const registryFromYarnrc = yarnrc?.npmPublishRegistry;

if (packageJson.private === true) {
ctx.logger.log('skipping since package is private');
Expand All @@ -33,9 +43,11 @@ export async function verifyConditions(
return;
}

if (registry) {
ctx.logger.log(`set npmRegistry to ${registry}`);
await yarn.setNpmRegistryServer(registry);
if (registryFromYarnrc || registryFromPackage) {
ctx.logger.log(
`set npmRegistry to ${registryFromYarnrc || registryFromPackage}`,
);
await yarn.setNpmRegistryServer(registryFromYarnrc || registryFromPackage);
} else {
ctx.logger.log('set npmRegistryServer: "https://registry.npmjs.org"');
await yarn.setNpmRegistryServer('https://registry.npmjs.org');
Expand All @@ -46,13 +58,15 @@ export async function verifyConditions(
await yarn.setNpmAuthToken(getNpmToken(ctx.env));

ctx.logger.log('verify npm auth');
if (!(await yarn.authenticated())) throw error(ErrorTypes.INVALID_NPM_TOKEN);
if (!(await yarn.authenticated()))
throw error(ErrorTypes.INVALID_NPM_TOKEN);
} else {
ctx.logger.log('set NPM_AUTH_IDENT to yarn config npmAuthIdent');
await yarn.setNpmAuthIdent(getNpmAuthIdent(ctx.env));

ctx.logger.log('verify npm auth');
if (!(await yarn.authenticated())) throw error(ErrorTypes.INVALID_NPM_AUTH_IDENT);
if (!(await yarn.authenticated()))
throw error(ErrorTypes.INVALID_NPM_AUTH_IDENT);
}

ctx.logger.log('install version plugin');
Expand Down
3 changes: 2 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export function getNpmToken(env: NodeJS.ProcessEnv): string {

export function getNpmAuthIdent(env: NodeJS.ProcessEnv): string {
const authIdent = env['NPM_AUTH_IDENT'];
if (typeof authIdent !== 'string') throw error(ErrorTypes.INVALID_NPM_AUTH_IDENT);
if (typeof authIdent !== 'string')
throw error(ErrorTypes.INVALID_NPM_AUTH_IDENT);
return authIdent;
}

Expand Down
23 changes: 23 additions & 0 deletions src/utils/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import tempy from 'tempy';
import * as fs from 'fs';
import { getPackage } from './index';

describe('utils', () => {
describe('getPackage', () => {
it('should return the package.json if it exists', async () => {
const cwd = tempy.directory();

const mockPackage = {
name: 'mypkg',
version: '1.0.0',
packageManager: 'yarn@3.4.1',
license: 'MIT',
};

fs.writeFileSync(`${cwd}/package.json`, JSON.stringify(mockPackage));
const result = await getPackage(cwd);

expect(result.name).toEqual(mockPackage.name);
});
});
});
26 changes: 21 additions & 5 deletions src/utils/yarn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ const YARN_RC_FILENAME = 'yarnrc.test.yaml';
const yarn = new Yarn({ HOME, YARN_RC_FILENAME });

describe('Yarn', () => {
const OLD_ENV = process.env;

beforeEach(() => {
jest.resetModules();
process.env = { ...OLD_ENV };
});

afterAll(() => {
process.env = OLD_ENV;
});

it('setNpmRegistryServer', async () => {
await yarn.setNpmRegistryServer('https://registry.npmjs.org');
expect(await yarn.getNpmRegistryServer()).toBe(
Expand Down Expand Up @@ -38,9 +49,6 @@ describe('Yarn', () => {
});

it('setNpmAuthIdent', async () => {
await yarn.setNpmRegistryServer(
'https://pkgs.dev.azure.com/allido/allido/_packaging/semantic-release-yarn/npm/registry/',
);
const authIdent = '__dummy_auth_ident__';
await yarn.setNpmAuthIdent(authIdent);
expect(await yarn.getNpmAuthIdent()).toBe(authIdent);
Expand Down Expand Up @@ -92,14 +100,21 @@ describe('Yarn', () => {
it('yarnPackDryRun', async () => {
const cwd = tempy.directory();
const packageJson = `${cwd}/package.json`;

fs.writeFileSync(
packageJson,
JSON.stringify({ version: '1.0.0', packageManager: 'yarn@3.1.0' }),
JSON.stringify({
name: 'mypkg',
version: '1.0.0',
packageManager: 'yarn@3.4.1',
license: 'MIT',
}),
);

fs.mkdirSync(`${cwd}/lib`);
fs.writeFileSync(`${cwd}/lib/index.js`, '');
const yarn = new Yarn({ HOME, YARN_RC_FILENAME, cwd });

const files = await yarn.packDryRun();
expect(files).toMatchInlineSnapshot(`
[
Expand All @@ -118,7 +133,8 @@ describe('Yarn', () => {
JSON.stringify({
name: 'mypkg',
version: '1.0.0',
packageManager: 'yarn@3.1.0',
packageManager: 'yarn@3.4.1',
license: 'MIT',
}),
);

Expand Down
Loading

0 comments on commit 80cad06

Please sign in to comment.