1 | (function (factory) {
|
2 | if (typeof module === "object" && typeof module.exports === "object") {
|
3 | var v = factory(require, exports);
|
4 | if (v !== undefined) module.exports = v;
|
5 | } else if (typeof define === "function" && define.amd) {
|
6 | define(["require", "exports", "path", "@spicypixel/core-kit-js", "./unity-project"], factory);
|
7 | }
|
8 | })(function (require, exports) {
|
9 | "use strict";
|
10 |
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | const path = require("path");
|
13 | const CoreKit = require("@spicypixel/core-kit-js");
|
14 | const unity_project_1 = require("./unity-project");
|
15 | class UnityModuleReference {
|
16 | constructor(module, nodeModule, nodeModuleVendor, nodeModuleName) {
|
17 | this._module = module;
|
18 | this._nodeModule = nodeModule;
|
19 | this._nodeModuleVendor = nodeModuleVendor;
|
20 | this._nodeModuleName = nodeModuleName;
|
21 | this._nodeModuleProject = new unity_project_1.default(nodeModule.packageDir);
|
22 | }
|
23 | get referencePath() {
|
24 | return path.resolve(path.join(this._nodeModuleProject.assetsPath, this._nodeModuleVendor, "Modules", this._nodeModuleName));
|
25 | }
|
26 | get installPath() {
|
27 | return path.resolve(path.join(this._module.project.assetsPath, this._nodeModuleVendor, "Modules", this._nodeModuleName));
|
28 | }
|
29 | get artifactsPath() {
|
30 | return path.join(this._nodeModuleProject.projectPath, "Artifacts");
|
31 | }
|
32 | async installAsync() {
|
33 | if (this.referencePath === this.installPath) {
|
34 | throw new Error("Source and destination cannot be the same: " + this.referencePath);
|
35 | }
|
36 | await this.uninstallAsync();
|
37 | const packageFileName = await this.getPackageFileName();
|
38 | await this._module.project.importPackageAsync(path.join(this.artifactsPath, packageFileName));
|
39 | }
|
40 | async uninstallAsync() {
|
41 |
|
42 |
|
43 | await CoreKit.FileSystem.removePatternsAsync(["**/*", "!**/*.meta"], { cwd: this.installPath, nodir: true });
|
44 |
|
45 | await CoreKit.FileSystem.Directory.removeUnmatchedAsync(this.referencePath, this.installPath, { ignoreMissingSource: true, ignoreMissingDestination: true });
|
46 | }
|
47 | async getPackageFileName() {
|
48 | const pkg = await CoreKit.FileSystem.File.readFileAsync(path.join(this._nodeModuleProject.projectPath, "package.json"), "utf8");
|
49 | const tag = "v" + JSON.parse(pkg).version;
|
50 | return this._nodeModuleVendor + "." + this._nodeModuleName + "-" + tag + ".unitypackage";
|
51 | }
|
52 | }
|
53 | exports.default = UnityModuleReference;
|
54 | });
|
55 |
|