From 55a9a83b1a460ab9070d503dfc18ffc00e3f618c Mon Sep 17 00:00:00 2001 From: Lawrence Forooghian Date: Mon, 30 Oct 2023 13:00:39 -0300 Subject: [PATCH] wip bundle install esbuild install Ably (TODO not like this) install tyepscript wip rename package test further --- .github/workflows/test-package.yml | 18 +++++ Gruntfile.js | 76 +++++++++++++++++-- package.json | 1 + .../browser/template/package-lock.json | 37 +++++++++ test/package/browser/template/package.json | 14 ++++ test/package/browser/template/src/index.ts | 8 ++ test/package/browser/template/tsconfig.json | 3 + tsconfig.json | 1 + 8 files changed, 153 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/test-package.yml create mode 100644 test/package/browser/template/package-lock.json create mode 100644 test/package/browser/template/package.json create mode 100644 test/package/browser/template/src/index.ts create mode 100644 test/package/browser/template/tsconfig.json diff --git a/.github/workflows/test-package.yml b/.github/workflows/test-package.yml new file mode 100644 index 0000000000..e18aebfe90 --- /dev/null +++ b/.github/workflows/test-package.yml @@ -0,0 +1,18 @@ +name: Test NPM package +on: + pull_request: + push: + branches: + - main + +jobs: + test-npm-package: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Use Node.js 16.x + uses: actions/setup-node@v1 + with: + node-version: 16.x + - run: npm ci + - run: npm run test:package diff --git a/Gruntfile.js b/Gruntfile.js index ca1f636f9a..66dea74c2d 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -6,6 +6,7 @@ var webpackConfig = require('./webpack.config'); var esbuild = require('esbuild'); var umdWrapper = require('esbuild-plugin-umd-wrapper'); var banner = require('./src/fragments/license'); +var process = require('process'); module.exports = function (grunt) { grunt.loadNpmTasks('grunt-contrib-concat'); @@ -27,18 +28,27 @@ module.exports = function (grunt) { }; } - function execExternal(cmd) { - return function () { - var done = this.async(); - grunt.log.ok('Executing ' + cmd); + async function execExternalPromises(cmd) { + grunt.log.ok('Executing ' + cmd); + return new Promise(function (resolve, reject) { require('child_process').exec(cmd, function (err, stdout, stderr) { if (err) { grunt.fatal('Error executing "' + cmd + '": ' + stderr); + reject(err); } console.log(stdout); stderr && console.error(stderr); - done(); + resolve(); }); + }); + } + + function execExternal(cmd) { + return function () { + var done = this.async(); + execExternalPromises(cmd) + .then(() => done()) + .catch((error) => done(error)); }; } @@ -204,5 +214,61 @@ module.exports = function (grunt) { } ); + grunt.registerTask('test:package:browser:prepare-project', function () { + const done = this.async(); + + (async function () { + const baseDir = path.join(__dirname, 'test', 'package', 'browser'); + const buildDir = path.join(baseDir, 'build'); + + if (grunt.file.exists(buildDir)) { + grunt.file.delete(buildDir); + } + + grunt.file.copy(path.join(baseDir, 'template'), buildDir); + + await execExternalPromises('npm run build'); + + await execExternalPromises('npm pack --pack-destination test/package/browser/build'); + const version = grunt.file.readJSON('package.json').version; + const packFileName = `ably-${version}.tgz`; + + const buildPackageJsonPath = 'test/package/browser/build/package.json'; + const projectPackageJson = grunt.file.readJSON(buildPackageJsonPath); + const dependencies = projectPackageJson.dependencies ?? {}; + dependencies.ably = `file:${packFileName}`; + projectPackageJson.dependencies = dependencies; + grunt.file.write(buildPackageJsonPath, JSON.stringify(projectPackageJson)); + + const pwd = process.cwd(); + process.chdir(buildDir); + await execExternalPromises('npm install'); + process.chdir(pwd); + })() + .then(() => done(true)) + .catch((error) => done(error)); + }); + + grunt.registerTask('test:package:browser:test', function () { + const done = this.async(); + + (async function () { + grunt.task.requires('test:package:browser:prepare-project'); + + const baseDir = path.join(__dirname, 'test', 'package', 'browser'); + const buildDir = path.join(baseDir, 'build'); + + const pwd = process.cwd(); + process.chdir(buildDir); + await execExternalPromises('npx tsc -noEmit'); + process.chdir(pwd); + })() + .then(() => done(true)) + .catch((error) => done(error)); + }); + + grunt.registerTask('test:package:browser', ['test:package:browser:prepare-project', 'test:package:browser:test']); + grunt.registerTask('test:package', ['test:package:browser']); + grunt.registerTask('default', 'all'); }; diff --git a/package.json b/package.json index 4c62961d7d..2f0ec20ef3 100644 --- a/package.json +++ b/package.json @@ -84,6 +84,7 @@ "test:node": "grunt test:node", "test:webserver": "grunt test:webserver", "test:playwright": "node test/support/runPlaywrightTests.js", + "test:package": "grunt test:package", "concat": "grunt concat", "build": "grunt build:all", "build:node": "grunt build:node", diff --git a/test/package/browser/template/package-lock.json b/test/package/browser/template/package-lock.json new file mode 100644 index 0000000000..294caa46b5 --- /dev/null +++ b/test/package/browser/template/package-lock.json @@ -0,0 +1,37 @@ +{ + "name": "template", + "version": "1.0.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "template", + "version": "1.0.0", + "license": "ISC", + "devDependencies": { + "typescript": "^5.2.2" + } + }, + "node_modules/typescript": { + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.2.2.tgz", + "integrity": "sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==", + "dev": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + } + }, + "dependencies": { + "typescript": { + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.2.2.tgz", + "integrity": "sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==", + "dev": true + } + } +} diff --git a/test/package/browser/template/package.json b/test/package/browser/template/package.json new file mode 100644 index 0000000000..6bcb112c86 --- /dev/null +++ b/test/package/browser/template/package.json @@ -0,0 +1,14 @@ +{ + "name": "template", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "devDependencies": { + "typescript": "^5.2.2" + } +} diff --git a/test/package/browser/template/src/index.ts b/test/package/browser/template/src/index.ts new file mode 100644 index 0000000000..8d3004fab8 --- /dev/null +++ b/test/package/browser/template/src/index.ts @@ -0,0 +1,8 @@ +import { Realtime } from 'ably'; + +(async () => { + const realtime = new Realtime({ key: '' }); + + const channel = realtime.channels.get('someChannel'); + await channel.attach(); +})(); diff --git a/test/package/browser/template/tsconfig.json b/test/package/browser/template/tsconfig.json new file mode 100644 index 0000000000..5d9f97afe5 --- /dev/null +++ b/test/package/browser/template/tsconfig.json @@ -0,0 +1,3 @@ +{ + "include": ["src/**/*.ts"] +} diff --git a/tsconfig.json b/tsconfig.json index 0c6561e653..f7a7af85cf 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,4 +1,5 @@ { + "exclude": ["test"], "compilerOptions": { "target": "es5", "module": "commonjs",