Skip to content

Commit

Permalink
wip bundle
Browse files Browse the repository at this point in the history
install esbuild

install Ably (TODO not like this)

install tyepscript

wip

rename

package test

further
  • Loading branch information
lawrence-forooghian committed Oct 30, 2023
1 parent c508a39 commit 55a9a83
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 5 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/test-package.yml
Original file line number Diff line number Diff line change
@@ -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
76 changes: 71 additions & 5 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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));
};
}

Expand Down Expand Up @@ -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');
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
37 changes: 37 additions & 0 deletions test/package/browser/template/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions test/package/browser/template/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
8 changes: 8 additions & 0 deletions test/package/browser/template/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Realtime } from 'ably';

(async () => {
const realtime = new Realtime({ key: '' });

const channel = realtime.channels.get('someChannel');
await channel.attach();
})();
3 changes: 3 additions & 0 deletions test/package/browser/template/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"include": ["src/**/*.ts"]
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"exclude": ["test"],
"compilerOptions": {
"target": "es5",
"module": "commonjs",
Expand Down

0 comments on commit 55a9a83

Please sign in to comment.