From 7fde74dbb32f3cc74802753a6f750347e255deaf Mon Sep 17 00:00:00 2001 From: Ivolutio Date: Mon, 8 Jun 2020 08:27:29 +0200 Subject: [PATCH 1/3] Use public REST API and support branchIds --- index.js | 46 +++++++++++++++++++++++++++++++++++----------- package.json | 34 +++++++++++++++++++--------------- 2 files changed, 54 insertions(+), 26 deletions(-) diff --git a/index.js b/index.js index bef5ca4..0963425 100644 --- a/index.js +++ b/index.js @@ -34,29 +34,53 @@ PlayCanvasWebpackPlugin.prototype.apply = function (compiler) { console.log("Uploading " + filename.path + " to PlayCanvas") let content = asset.children.map(c => c._value ? c._value : c).join('\n') let req = request({ - uri: `https://playcanvas.com/api/assets`, - method: 'POST', + uri: `https://playcanvas.com/api/assets/${filename.assetId}`, + method: 'PUT', headers: { "Authorization": `Bearer ${options.bearer}` } }) let form = req.form() - form.append("project", "" + options.project) - form.append("name", "" + filename.path) - form.append("asset", "" + filename.assetId) - form.append("data", JSON.stringify({order: filename.priority || 100, scripts: {}})) - form.append("preload", "true") + form.append("branchId", "" + options.branchId) form.append("file", content, { filename: filename.path, contentType: "text/javascript" }) req.then(() => { - console.log("Upload complete for file " + filename.path) + console.log("Upload complete for file (update) " + filename.path) callback() }, (e) => { - console.error(e) - compilation.errors.push(e) - callback() + if(e.statusCode === 404){ + let req = request({ + uri: `https://playcanvas.com/api/assets`, + method: 'POST', + headers: { + "Authorization": `Bearer ${options.bearer}` + } + }) + let form = req.form() + form.append("name", "" + filename.path) + form.append("project", "" + options.project) + form.append("branchId", "" + options.branchId) + form.append("preload", "true") + form.append("file", content, { + filename: filename.path, + contentType: "text/javascript" + }) + req.then((res) => { + console.log("Upload complete for file (create) " + filename.path) + compilation.warnings.push("PlayCanvas Webpack Plugin\nNew main.build.js has been created. Update assetId config to" + JSON.parse(res).id) + callback() + }, (e) => { + console.error(e) + compilation.errors.push(e) + callback() + }) + }else{ + console.error(e) + compilation.errors.push(e) + callback() + } }) diff --git a/package.json b/package.json index 5751b6a..61569f4 100644 --- a/package.json +++ b/package.json @@ -1,29 +1,33 @@ { - "name": "playcanvas-webpack-plugin", - "version": "1.0.8", - "description": "WebPack plugin to upload built script files to PlayCanvas.", - "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "author": { + "name": "Mike Talbot" }, - "author": "Mike Talbot", - "license": "ISC", + "bugs": { + "url": "https://github.com/whydoidoit/playcanvas-webpack-plugin/issues" + }, + "bundleDependencies": false, "dependencies": { "lodash": "^4.17.4", "request": "^2.81.0", "request-promise": "^4.2.1" }, + "deprecated": false, + "description": "WebPack plugin to upload built script files to PlayCanvas.", "devDependencies": {}, - "repository": { - "type": "git", - "url": "git+https://github.com/whydoidoit/playcanvas-webpack-plugin.git" - }, + "homepage": "https://github.com/whydoidoit/playcanvas-webpack-plugin#readme", "keywords": [ "PlayCanvas", "WebPack" ], - "bugs": { - "url": "https://github.com/whydoidoit/playcanvas-webpack-plugin/issues" + "license": "ISC", + "main": "index.js", + "name": "playcanvas-webpack-plugin", + "repository": { + "type": "git", + "url": "git+https://github.com/whydoidoit/playcanvas-webpack-plugin.git" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" }, - "homepage": "https://github.com/whydoidoit/playcanvas-webpack-plugin#readme" + "version": "1.0.8" } From a2e3e1852292d7b08ce926542c217c80039edfb8 Mon Sep 17 00:00:00 2001 From: Ivolutio Date: Mon, 8 Jun 2020 08:41:02 +0200 Subject: [PATCH 2/3] Update Readme --- .gitignore | 1 + package.json | 2 +- readme.md | 13 ++++++++----- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 3c3629e..67f1473 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +*.npmignore diff --git a/package.json b/package.json index 61569f4..e64c09d 100644 --- a/package.json +++ b/package.json @@ -29,5 +29,5 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, - "version": "1.0.8" + "version": "1.0.9" } diff --git a/readme.md b/readme.md index 545aa6b..9ed4e6f 100644 --- a/readme.md +++ b/readme.md @@ -12,17 +12,19 @@ npm install --save playcanvas-webpack-plugin 1. Build your WebPack output one time. -2. Go to the PlayCanvas editor for your project on the web and open your developer tools. +2. Go to your account settings and scroll down to API Tokens and generate a new one. -3. Drop the build file into the assets window. +2. Go to the project overview page and copy the project id from the URL. -4. In the javascript console type config.accessToken - use this as your bearer token later +3. Go to the PlayCanvas editor for your project on the web. -5. In the javascript console type config.project.id - use this as your project id later +4. Go to version control and note the branch id of the current/wanted branch. + +5. Drop the build file into the assets window. 6. Select the build file you dropped in the assets window of the PlayCanvas editor and note its Asset Id in the properties panel. Use this in your WebPack configuration along with -the bearer token and the project id. +the API token, branch id and the project id. 7. In your webpack config add the plugin and configure its options: @@ -56,6 +58,7 @@ module.exports = { bearer: 'YOUR_BEARER_TOKEN', // From the step above project: YOUR_PROJECT_ID, // From the step above + branchid: YOUR_BRANCH_ID, // From the step above files: { "your_build.output.js": { //Name of your build output path: "your_build.output.js", //Name in PlayCanvas, normally the same From e777859ad58effb6ee60598ff71f29742cbbf23f Mon Sep 17 00:00:00 2001 From: Ivolutio Date: Mon, 8 Jun 2020 08:44:54 +0200 Subject: [PATCH 3/3] update package.json order --- package.json | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index e64c09d..415e464 100644 --- a/package.json +++ b/package.json @@ -1,33 +1,33 @@ { + "name": "playcanvas-webpack-plugin", + "version": "1.0.9", + "description": "WebPack plugin to upload built script files to PlayCanvas.", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, "author": { "name": "Mike Talbot" }, - "bugs": { - "url": "https://github.com/whydoidoit/playcanvas-webpack-plugin/issues" - }, - "bundleDependencies": false, + "license": "ISC", "dependencies": { "lodash": "^4.17.4", "request": "^2.81.0", "request-promise": "^4.2.1" }, - "deprecated": false, - "description": "WebPack plugin to upload built script files to PlayCanvas.", "devDependencies": {}, - "homepage": "https://github.com/whydoidoit/playcanvas-webpack-plugin#readme", - "keywords": [ - "PlayCanvas", - "WebPack" - ], - "license": "ISC", - "main": "index.js", - "name": "playcanvas-webpack-plugin", "repository": { "type": "git", "url": "git+https://github.com/whydoidoit/playcanvas-webpack-plugin.git" }, - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "keywords": [ + "PlayCanvas", + "WebPack" + ], + "bugs": { + "url": "https://github.com/whydoidoit/playcanvas-webpack-plugin/issues" }, - "version": "1.0.9" + "homepage": "https://github.com/whydoidoit/playcanvas-webpack-plugin#readme", + "bundleDependencies": false, + "deprecated": false }