Skip to content

Commit

Permalink
Let GitHub Actions write package jsons for bento (#34311)
Browse files Browse the repository at this point in the history
* wnode script

* error handle

* log

* move files

* pr comments
  • Loading branch information
estherkim authored May 12, 2021
1 parent e6f307e commit e16d1e7
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*/

const [extension] = process.argv.slice(2);
const {timedExecOrDie} = require('./utils');
const {updatePackages} = require('../common/update-packages');
const {timedExecOrDie} = require('../../../build-system/pr-check/utils');
const {updatePackages} = require('../../../build-system/common/update-packages');

updatePackages();
timedExecOrDie(`amp build --extensions=${extension} --core_runtime_only`);
Expand Down
105 changes: 105 additions & 0 deletions .github/workflows/publish/write-package-jsons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* Copyright 2021 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @fileoverview
* Creates package.json files for a given component and AMP version to be
* published on npm.
*/

const [extension, ampVersion] = process.argv.slice(2);
const {log} = require('../../../build-system/common/logging');
const {stat, writeFile} = require('fs/promises');
const {valid} = require('semver');

async function writePackageJson(extensionVersion) {
try {
await stat(`extensions/${extension}/${extensionVersion}`);
} catch {
log(`${extension} ${extensionVersion} : skipping, does not exist`);
return;
}

const extensionVersionArr = extensionVersion.split('.', 2);
const major = extensionVersionArr[0];
const minor = ampVersion.slice(0, 10);
const patch = Number(ampVersion.slice(-3)); // npm trims leading zeroes in patch number, so mimic this in package.json
const version = `${major}.${minor}.${patch}`;
if (!valid(version) || ampVersion.length != 13 || extensionVersionArr[1] !== '0') {
log(
'Invalid semver version',
version,
'or AMP version',
ampVersion,
'or extension version',
extensionVersion
);
process.exitCode = 1;
return;
}

const json = {
name: `@ampproject/${extension}`,
version,
description: `AMP HTML ${extension} Component`,
author: 'The AMP HTML Authors',
license: 'Apache-2.0',
main: 'dist/component.js',
module: 'dist/component.mjs',
exports: {
'.': './preact',
'./preact': {
import: 'dist/component-preact.mjs',
require: 'dist/component-preact.js',
},
'./react': {
import: 'dist/component-react.mjs',
require: 'dist/component-react.js',
},
},
files: ['dist/*'],
repository: {
type: 'git',
url: 'https://github.com/ampproject/amphtml.git',
directory: `extensions/${extension}/${extensionVersion}`,
},
homepage: `https://github.com/ampproject/amphtml/tree/main/extensions/${extension}/${extensionVersion}`,
peerDependencies: {
preact: '^10.2.1',
react: '^17.0.0',
},
};

try {
await writeFile(
`extensions/${extension}/${extensionVersion}/package.json`,
JSON.stringify(json, null, 2)
);
log(
extension,
extensionVersion,
': created package.json for',
json.version
);
} catch (e) {
log(e);
process.exitCode = 1;
return;
}
}

writePackageJson('1.0');
writePackageJson('2.0');

0 comments on commit e16d1e7

Please sign in to comment.