diff --git a/.gitignore b/.gitignore index 3c3629e..67f1473 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +*.npmignore 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..415e464 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,14 @@ { "name": "playcanvas-webpack-plugin", - "version": "1.0.8", + "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": "Mike Talbot", + "author": { + "name": "Mike Talbot" + }, "license": "ISC", "dependencies": { "lodash": "^4.17.4", @@ -25,5 +27,7 @@ "bugs": { "url": "https://github.com/whydoidoit/playcanvas-webpack-plugin/issues" }, - "homepage": "https://github.com/whydoidoit/playcanvas-webpack-plugin#readme" + "homepage": "https://github.com/whydoidoit/playcanvas-webpack-plugin#readme", + "bundleDependencies": false, + "deprecated": false } 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