@wocker/ws 1.0.15 → 1.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/AppModule.js CHANGED
@@ -39,9 +39,9 @@ let AppModule = AppModule_1 = class AppModule {
39
39
  const appConfigService = container.getModule(AppModule_1).get(services_1.AppConfigService);
40
40
  const logService = container.getModule(AppModule_1).get(services_1.LogService);
41
41
  const pluginService = container.getModule(AppModule_1).get(services_1.PluginService);
42
- const config = await appConfigService.getConfig();
42
+ const config = appConfigService.getConfig();
43
43
  const imports = [];
44
- for (const plugin of config.plugins) {
44
+ for (const plugin of config.plugins || []) {
45
45
  try {
46
46
  const { default: Plugin } = await Promise.resolve(`${plugin}`).then(s => __importStar(require(s)));
47
47
  if (!Plugin) {
@@ -4,5 +4,8 @@ export declare class DebugController {
4
4
  protected readonly logService: LogService;
5
5
  constructor(appConfigService: AppConfigService, logService: LogService);
6
6
  debug(status: string): Promise<void>;
7
+ setLog(level: string): Promise<void>;
8
+ testLog(level: string, args: string[]): Promise<void>;
7
9
  debugCompletion(): Promise<string[]>;
10
+ getLevels(): string[];
8
11
  }
@@ -8,6 +8,9 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
8
8
  var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
10
  };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  exports.DebugController = void 0;
13
16
  const core_1 = require("@wocker/core");
@@ -18,27 +21,66 @@ let DebugController = class DebugController {
18
21
  this.logService = logService;
19
22
  }
20
23
  async debug(status) {
21
- const config = await this.appConfigService.getConfig();
24
+ const config = this.appConfigService.getConfig();
22
25
  config.debug = status === "on";
23
26
  await config.save();
24
27
  }
28
+ async setLog(level) {
29
+ const validLevels = this.getLevels();
30
+ if (!validLevels.includes(level)) {
31
+ throw new Error(`Invalid log level: ${level}. Valid options are ${validLevels.join(', ')}`);
32
+ }
33
+ const config = this.appConfigService.getConfig();
34
+ config.logLevel = level;
35
+ await config.save();
36
+ }
37
+ async testLog(level, args) {
38
+ this.logService._log(level, ...args);
39
+ }
25
40
  async debugCompletion() {
26
41
  return ["on", "off"];
27
42
  }
43
+ getLevels() {
44
+ return ["debug", "info", "warn", "error"];
45
+ }
28
46
  };
29
47
  exports.DebugController = DebugController;
30
48
  __decorate([
49
+ (0, core_1.Command)("debug:<status>"),
31
50
  (0, core_1.Command)("debug <status>"),
51
+ __param(0, (0, core_1.Param)("status")),
32
52
  __metadata("design:type", Function),
33
53
  __metadata("design:paramtypes", [String]),
34
54
  __metadata("design:returntype", Promise)
35
55
  ], DebugController.prototype, "debug", null);
56
+ __decorate([
57
+ (0, core_1.Description)("Set the log level (options: debug, log, info, warn, error)"),
58
+ (0, core_1.Command)("loglevel <level>"),
59
+ __param(0, (0, core_1.Param)("level")),
60
+ __metadata("design:type", Function),
61
+ __metadata("design:paramtypes", [String]),
62
+ __metadata("design:returntype", Promise)
63
+ ], DebugController.prototype, "setLog", null);
64
+ __decorate([
65
+ (0, core_1.Command)("log:<level> [...args]"),
66
+ __param(0, (0, core_1.Param)("level")),
67
+ __param(1, (0, core_1.Param)("args")),
68
+ __metadata("design:type", Function),
69
+ __metadata("design:paramtypes", [String, Array]),
70
+ __metadata("design:returntype", Promise)
71
+ ], DebugController.prototype, "testLog", null);
36
72
  __decorate([
37
73
  (0, core_1.Completion)("status"),
38
74
  __metadata("design:type", Function),
39
75
  __metadata("design:paramtypes", []),
40
76
  __metadata("design:returntype", Promise)
41
77
  ], DebugController.prototype, "debugCompletion", null);
78
+ __decorate([
79
+ (0, core_1.Completion)("level"),
80
+ __metadata("design:type", Function),
81
+ __metadata("design:paramtypes", []),
82
+ __metadata("design:returntype", Array)
83
+ ], DebugController.prototype, "getLevels", null);
42
84
  exports.DebugController = DebugController = __decorate([
43
85
  (0, core_1.Controller)(),
44
86
  __metadata("design:paramtypes", [services_1.AppConfigService,
@@ -11,6 +11,7 @@ export declare class PresetController {
11
11
  protected onInit(project: Project): Promise<void>;
12
12
  protected onRebuild(project: Project): Promise<void>;
13
13
  protected onBeforeStart(project: Project): Promise<void>;
14
+ init(): Promise<void>;
14
15
  add(name: string): Promise<void>;
15
16
  delete(name: string, confirm?: boolean): Promise<void>;
16
17
  eject(name?: string): Promise<void>;
@@ -50,7 +50,6 @@ let PresetController = class PresetController {
50
50
  this.projectService = projectService;
51
51
  this.presetService = presetService;
52
52
  this.dockerService = dockerService;
53
- this.appConfigService.registerProjectType("preset", "Preset");
54
53
  this.appEventsService.on("project:init", (project) => this.onInit(project));
55
54
  this.appEventsService.on("project:beforeStart", (project) => this.onBeforeStart(project));
56
55
  this.appEventsService.on("project:rebuild", (project) => this.onRebuild(project));
@@ -62,7 +61,7 @@ let PresetController = class PresetController {
62
61
  });
63
62
  }
64
63
  async onInit(project) {
65
- if (project.type !== "preset") {
64
+ if (project.type !== core_1.PROJECT_TYPE_PRESET) {
66
65
  return;
67
66
  }
68
67
  const presets = await this.presetService.search();
@@ -149,6 +148,9 @@ let PresetController = class PresetController {
149
148
  }
150
149
  }
151
150
  }
151
+ async init() {
152
+ await this.presetService.init();
153
+ }
152
154
  async add(name) {
153
155
  await this.presetService.addPreset(name);
154
156
  }
@@ -182,7 +184,7 @@ let PresetController = class PresetController {
182
184
  if (!confirm) {
183
185
  return;
184
186
  }
185
- const copier = new core_1.FSManager(this.appConfigService.presetPath(preset.name), this.appConfigService.getPWD());
187
+ const copier = new core_1.FSManager(this.appConfigService.presetPath(preset.name), this.appConfigService.pwd());
186
188
  if (preset.dockerfile) {
187
189
  if (!copier.destination.exists(preset.dockerfile)) {
188
190
  await copier.copy(preset.dockerfile);
@@ -234,6 +236,12 @@ let PresetController = class PresetController {
234
236
  }
235
237
  };
236
238
  exports.PresetController = PresetController;
239
+ __decorate([
240
+ (0, core_1.Command)("preset:init"),
241
+ __metadata("design:type", Function),
242
+ __metadata("design:paramtypes", []),
243
+ __metadata("design:returntype", Promise)
244
+ ], PresetController.prototype, "init", null);
237
245
  __decorate([
238
246
  (0, core_1.Command)("preset:add <preset>"),
239
247
  __param(0, (0, core_1.Param)("preset")),
@@ -1,3 +1,4 @@
1
+ import { ProjectType } from "@wocker/core";
1
2
  import { AppConfigService, AppEventsService, ProjectService, DockerService, LogService } from "../services";
2
3
  export declare class ProjectController {
3
4
  protected readonly appConfigService: AppConfigService;
@@ -8,7 +9,7 @@ export declare class ProjectController {
8
9
  constructor(appConfigService: AppConfigService, appEventsService: AppEventsService, projectService: ProjectService, dockerService: DockerService, logService: LogService);
9
10
  protected getProjectNames(): Promise<string[]>;
10
11
  getScriptNames(): Promise<string[]>;
11
- init(name: string, type: string, preset: string): Promise<void>;
12
+ init(name: string, type: ProjectType, preset: string): Promise<void>;
12
13
  projectList(all: boolean): Promise<string>;
13
14
  start(name?: string, detach?: boolean, attach?: boolean, rebuild?: boolean, restart?: boolean): Promise<void>;
14
15
  stop(name: string): Promise<void>;
@@ -73,13 +73,15 @@ let ProjectController = class ProjectController {
73
73
  }
74
74
  async init(name, type, preset) {
75
75
  let project = await this.projectService.searchOne({
76
- path: this.appConfigService.getPWD()
76
+ path: this.appConfigService.pwd()
77
77
  });
78
+ const fs = new core_1.FileSystem(this.appConfigService.pwd());
78
79
  if (!project) {
79
80
  project = this.projectService.fromObject({
80
- path: this.appConfigService.getPWD()
81
+ path: this.appConfigService.pwd()
81
82
  });
82
83
  }
84
+ project.path = this.appConfigService.pwd();
83
85
  if (name) {
84
86
  project.name = name;
85
87
  }
@@ -97,15 +99,15 @@ let ProjectController = class ProjectController {
97
99
  }
98
100
  const mapTypes = this.appConfigService.getProjectTypes();
99
101
  if (!type || !project.type || !mapTypes[project.type]) {
100
- project.type = await (0, utils_1.promptSelect)({
102
+ project.type = (await (0, utils_1.promptSelect)({
101
103
  message: "Project type:",
102
104
  options: mapTypes,
103
105
  default: project.type
104
- });
106
+ }));
105
107
  }
106
108
  switch (project.type) {
107
109
  case core_1.PROJECT_TYPE_DOCKERFILE: {
108
- const files = await makes_1.FS.readdirFiles(this.appConfigService.getPWD());
110
+ const files = await fs.readdirFiles();
109
111
  const dockerfiles = files.filter((fileName) => {
110
112
  if (new RegExp("^(.*)\\.dockerfile$").test(fileName)) {
111
113
  return true;
@@ -113,28 +115,30 @@ let ProjectController = class ProjectController {
113
115
  return new RegExp("^Dockerfile(\\..*)?").test(fileName);
114
116
  });
115
117
  project.dockerfile = await (0, utils_1.promptSelect)({
118
+ message: "Dockerfile:",
116
119
  options: dockerfiles.map((dockerfile) => {
117
120
  return {
118
121
  value: dockerfile
119
122
  };
120
123
  }),
121
- message: "Dockerfile",
122
124
  default: project.dockerfile
123
125
  });
124
126
  break;
125
127
  }
126
128
  case core_1.PROJECT_TYPE_IMAGE: {
127
129
  project.imageName = await (0, utils_1.promptText)({
128
- message: "Image Name",
130
+ message: "Image name:",
131
+ required: true,
129
132
  default: project.imageName
130
133
  });
131
134
  break;
132
135
  }
133
- default:
136
+ case core_1.PROJECT_TYPE_PRESET:
134
137
  break;
138
+ default:
139
+ throw new Error("Invalid project type");
135
140
  }
136
141
  await this.appEventsService.emit("project:init", project);
137
- project.path = this.appConfigService.getPWD();
138
142
  await project.save();
139
143
  }
140
144
  async projectList(all) {
package/lib/env.d.ts CHANGED
@@ -4,5 +4,4 @@ export declare const PRESETS_DIR: string;
4
4
  export declare const SERVICES_DIR: string;
5
5
  export declare const PLUGINS_DIR: string;
6
6
  export declare const DATA_DIR: string;
7
- export declare const MAP_PATH: string;
8
7
  export declare const VIRTUAL_HOST_KEY = "VIRTUAL_HOST";
package/lib/env.js CHANGED
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.VIRTUAL_HOST_KEY = exports.MAP_PATH = exports.DATA_DIR = exports.PLUGINS_DIR = exports.SERVICES_DIR = exports.PRESETS_DIR = exports.ROOT_DIR = exports.NODE_ENV = void 0;
26
+ exports.VIRTUAL_HOST_KEY = exports.DATA_DIR = exports.PLUGINS_DIR = exports.SERVICES_DIR = exports.PRESETS_DIR = exports.ROOT_DIR = exports.NODE_ENV = void 0;
27
27
  const OS = __importStar(require("os"));
28
28
  const Path = __importStar(require("path"));
29
29
  exports.NODE_ENV = process.env.NODE_ENV;
@@ -32,5 +32,4 @@ exports.PRESETS_DIR = Path.join(exports.ROOT_DIR, "presets");
32
32
  exports.SERVICES_DIR = Path.join(exports.ROOT_DIR, "services");
33
33
  exports.PLUGINS_DIR = Path.join(exports.ROOT_DIR, "plugins");
34
34
  exports.DATA_DIR = process.env.WS_DIR || Path.join(OS.homedir(), ".workspace");
35
- exports.MAP_PATH = Path.join(exports.DATA_DIR, "data.json");
36
35
  exports.VIRTUAL_HOST_KEY = "VIRTUAL_HOST";
@@ -1,18 +1,18 @@
1
- import { Config, AppConfigService as CoreAppConfigService } from "@wocker/core";
1
+ import { AppConfig, AppConfigService as CoreAppConfigService } from "@wocker/core";
2
2
  type TypeMap = {
3
3
  [type: string]: string;
4
4
  };
5
5
  export declare class AppConfigService extends CoreAppConfigService {
6
- protected pwd: string;
6
+ protected _pwd: string;
7
7
  protected mapTypes: TypeMap;
8
8
  constructor();
9
+ setPWD(pwd: string): void;
10
+ getPWD(...parts: string[]): string;
11
+ pwd(...parts: string[]): string;
9
12
  dataPath(...parts: string[]): string;
10
13
  pluginsPath(...parts: string[]): string;
11
14
  presetPath(...parts: string[]): string;
12
- getPWD(): string;
13
- setPWD(pwd: string): void;
14
15
  getProjectTypes(): TypeMap;
15
- registerProjectType(name: string, title?: string): void;
16
- protected loadConfig(): Promise<Config>;
16
+ protected loadConfig(): AppConfig;
17
17
  }
18
18
  export {};
@@ -36,15 +36,24 @@ exports.AppConfigService = void 0;
36
36
  const core_1 = require("@wocker/core");
37
37
  const Path = __importStar(require("path"));
38
38
  const env_1 = require("../env");
39
- const makes_1 = require("../makes");
40
39
  let AppConfigService = class AppConfigService extends core_1.AppConfigService {
41
40
  constructor() {
42
41
  super();
43
42
  this.mapTypes = {
44
- image: "Image",
45
- dockerfile: "Dockerfile"
43
+ [core_1.PROJECT_TYPE_IMAGE]: "Image",
44
+ [core_1.PROJECT_TYPE_DOCKERFILE]: "Dockerfile",
45
+ [core_1.PROJECT_TYPE_PRESET]: "Preset"
46
46
  };
47
- this.pwd = (process.cwd() || process.env.PWD);
47
+ this._pwd = (process.cwd() || process.env.PWD);
48
+ }
49
+ setPWD(pwd) {
50
+ this._pwd = pwd;
51
+ }
52
+ getPWD(...parts) {
53
+ return this.pwd(...parts);
54
+ }
55
+ pwd(...parts) {
56
+ return Path.join(this._pwd, ...parts);
48
57
  }
49
58
  dataPath(...parts) {
50
59
  return Path.join(env_1.DATA_DIR, ...parts);
@@ -55,37 +64,43 @@ let AppConfigService = class AppConfigService extends core_1.AppConfigService {
55
64
  presetPath(...parts) {
56
65
  return Path.join(env_1.PRESETS_DIR, ...parts);
57
66
  }
58
- getPWD() {
59
- return this.pwd;
60
- }
61
- setPWD(pwd) {
62
- this.pwd = pwd;
63
- }
64
67
  getProjectTypes() {
65
68
  return this.mapTypes;
66
69
  }
67
- registerProjectType(name, title) {
68
- this.mapTypes[name] = title || name;
69
- }
70
- async loadConfig() {
71
- const data = makes_1.FS.existsSync(env_1.MAP_PATH)
72
- ? await makes_1.FS.readJSON(env_1.MAP_PATH)
73
- : {};
74
- return new class extends core_1.Config {
70
+ loadConfig() {
71
+ const fs = new core_1.FileSystem(env_1.DATA_DIR);
72
+ let data = {};
73
+ if (fs.exists("wocker.config.js")) {
74
+ try {
75
+ const { config } = require(fs.path("wocker.config.js"));
76
+ data = config;
77
+ }
78
+ catch (err) {
79
+ if (fs.exists("wocker.json")) {
80
+ data = fs.readJSON("wocker.json");
81
+ }
82
+ }
83
+ }
84
+ else if (fs.exists("wocker.json")) {
85
+ data = fs.readJSON("wocker.json");
86
+ }
87
+ else if (fs.exists("data.json")) {
88
+ data = fs.readJSON("data.json");
89
+ }
90
+ return new class extends core_1.AppConfig {
75
91
  constructor(data) {
76
92
  super(data);
77
93
  }
78
- addPlugin(plugin) {
79
- if (!this.plugins) {
80
- this.plugins = [];
94
+ async save() {
95
+ if (!fs.exists()) {
96
+ fs.mkdir("");
81
97
  }
82
- if (this.plugins.includes(plugin)) {
83
- return;
98
+ const json = JSON.stringify(this.toJson(), null, 4);
99
+ await fs.writeFile("wocker.config.js", `// Wocker config\nexports.config = ${json};`);
100
+ await fs.writeJSON("wocker.json", json);
101
+ if (fs.exists("data.json")) {
102
+ await fs.rm("data.json");
84
103
  }
85
- this.plugins.push(plugin);
86
- }
87
- async save() {
88
- await makes_1.FS.writeJSON(env_1.MAP_PATH, this.toJson());
89
104
  }
90
105
  }(data);
91
106
  }
@@ -1,7 +1,9 @@
1
+ import { LogService as CoreLogService } from "@wocker/core";
1
2
  import { AppConfigService } from "./AppConfigService";
2
- export declare class LogService {
3
+ export declare class LogService extends CoreLogService {
3
4
  protected readonly appConfigService: AppConfigService;
4
5
  constructor(appConfigService: AppConfigService);
6
+ debug(...data: any[]): void;
5
7
  log(...data: any[]): void;
6
8
  info(...data: any[]): void;
7
9
  warn(...data: any[]): void;
@@ -17,11 +17,15 @@ const core_1 = require("@wocker/core");
17
17
  const format_1 = __importDefault(require("date-fns/format"));
18
18
  const makes_1 = require("../makes");
19
19
  const AppConfigService_1 = require("./AppConfigService");
20
- let LogService = class LogService {
20
+ let LogService = class LogService extends core_1.LogService {
21
21
  constructor(appConfigService) {
22
+ super();
22
23
  this.appConfigService = appConfigService;
23
24
  makes_1.Logger.install(this);
24
25
  }
26
+ debug(...data) {
27
+ this._log("debug", ...data);
28
+ }
25
29
  log(...data) {
26
30
  this._log("log", ...data);
27
31
  }
@@ -35,6 +39,10 @@ let LogService = class LogService {
35
39
  this._log("error", ...data);
36
40
  }
37
41
  _log(type, ...data) {
42
+ const config = this.appConfigService.getConfig();
43
+ if (type === "debug" && !config.debug) {
44
+ return;
45
+ }
38
46
  const time = (0, format_1.default)(new Date(), "yyyy-MM-dd hh:mm:ss");
39
47
  const logPath = this.appConfigService.dataPath("ws.log");
40
48
  const logData = data.map((item) => {
@@ -1,14 +1,19 @@
1
- import { EnvConfig, Preset, Config, PresetProperties, AppConfigService } from "@wocker/core";
1
+ import { EnvConfig, Preset, AppConfig, PresetProperties } from "@wocker/core";
2
+ import { AppConfigService } from "./AppConfigService";
3
+ import { LogService } from "./LogService";
2
4
  type SearchOptions = Partial<{
3
5
  name: string;
4
6
  source: string;
7
+ path: string;
5
8
  }>;
6
9
  export declare class PresetService {
7
10
  protected readonly appConfigService: AppConfigService;
8
- constructor(appConfigService: AppConfigService);
11
+ protected readonly logService: LogService;
12
+ constructor(appConfigService: AppConfigService, logService: LogService);
9
13
  protected toObject(config: PresetProperties): Preset;
10
- protected getList(): Promise<Config["presets"]>;
14
+ protected getList(): Promise<AppConfig["presets"]>;
11
15
  getImageName(preset: Preset, buildArgs?: EnvConfig): string;
16
+ init(): Promise<void>;
12
17
  get(name: string): Promise<Preset>;
13
18
  addPreset(name: string): Promise<void>;
14
19
  search(options?: SearchOptions): Promise<Preset[]>;
@@ -37,15 +37,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
37
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
38
  exports.PresetService = void 0;
39
39
  const core_1 = require("@wocker/core");
40
+ const utils_1 = require("@wocker/utils");
40
41
  const md5_1 = __importDefault(require("md5"));
41
42
  const axios_1 = __importDefault(require("axios"));
42
43
  const unzipper_1 = require("unzipper");
43
44
  const Path = __importStar(require("path"));
45
+ const AppConfigService_1 = require("./AppConfigService");
46
+ const LogService_1 = require("./LogService");
44
47
  const env_1 = require("../env");
45
48
  const makes_1 = require("../makes");
46
49
  let PresetService = class PresetService {
47
- constructor(appConfigService) {
50
+ constructor(appConfigService, logService) {
48
51
  this.appConfigService = appConfigService;
52
+ this.logService = logService;
49
53
  }
50
54
  toObject(config) {
51
55
  const _this = this;
@@ -55,24 +59,32 @@ let PresetService = class PresetService {
55
59
  }
56
60
  async save() {
57
61
  const { source, path, ...rest } = this.toJSON();
58
- const config = await _this.appConfigService.getConfig();
62
+ const config = _this.appConfigService.getConfig();
59
63
  let presetData = config.presets.find((presetData) => {
60
64
  return presetData.name === this.name;
61
65
  });
62
- if (!makes_1.FS.existsSync(_this.appConfigService.dataPath("presets", this.name))) {
63
- makes_1.FS.mkdirSync(_this.appConfigService.dataPath("presets", this.name), {
64
- recursive: true
65
- });
66
+ switch (this.source) {
67
+ case core_1.PRESET_SOURCE_EXTERNAL:
68
+ const fs = new core_1.FileSystem(this.path);
69
+ await fs.writeJSON("config.json", rest);
70
+ break;
71
+ case core_1.PRESET_SOURCE_GITHUB: {
72
+ const fs = new core_1.FileSystem(_this.appConfigService.dataPath("presets", this.name));
73
+ if (!fs.exists()) {
74
+ fs.mkdir("");
75
+ }
76
+ await fs.writeJSON("config.json", rest);
77
+ break;
78
+ }
66
79
  }
67
- await makes_1.FS.writeJSON(_this.appConfigService.dataPath("presets", this.name, "config.json"), rest);
68
80
  if (!presetData) {
69
- config.registerPreset(this.name, source);
81
+ config.registerPreset(this.name, source, path);
70
82
  await config.save();
71
83
  }
72
84
  }
73
85
  async delete() {
74
86
  if (this.source === core_1.PRESET_SOURCE_GITHUB) {
75
- const config = await _this.appConfigService.getConfig();
87
+ const config = _this.appConfigService.getConfig();
76
88
  await makes_1.FS.rm(_this.appConfigService.dataPath("presets", this.name), {
77
89
  recursive: true
78
90
  });
@@ -84,7 +96,7 @@ let PresetService = class PresetService {
84
96
  }
85
97
  async getList() {
86
98
  const dirs = await makes_1.FS.readdir(env_1.PRESETS_DIR);
87
- const { presets } = await this.appConfigService.getConfig();
99
+ const { presets } = this.appConfigService.getConfig();
88
100
  return [
89
101
  ...dirs.map((name) => {
90
102
  return {
@@ -93,7 +105,12 @@ let PresetService = class PresetService {
93
105
  path: Path.join(env_1.PRESETS_DIR, name)
94
106
  };
95
107
  }),
96
- ...presets
108
+ ...presets.map((item) => {
109
+ if (item.source === core_1.PRESET_SOURCE_GITHUB) {
110
+ item.path = this.appConfigService.dataPath("presets", item.name);
111
+ }
112
+ return item;
113
+ })
97
114
  ];
98
115
  }
99
116
  getImageName(preset, buildArgs = {}) {
@@ -117,6 +134,92 @@ let PresetService = class PresetService {
117
134
  }).join("-");
118
135
  return `ws-preset-${preset.name}:${version}`;
119
136
  }
137
+ async init() {
138
+ let preset = await this.searchOne({
139
+ path: this.appConfigService.pwd()
140
+ });
141
+ if (preset) {
142
+ throw new Error("Preset is already registered");
143
+ }
144
+ const fs = new core_1.FileSystem(this.appConfigService.pwd());
145
+ if (!fs.exists("config.json")) {
146
+ preset = this.toObject({
147
+ name: fs.basename(),
148
+ version: "1.0.0",
149
+ source: "external",
150
+ path: this.appConfigService.pwd()
151
+ });
152
+ const list = await this.getList();
153
+ preset.name = await (0, utils_1.promptText)({
154
+ message: "Preset name:",
155
+ required: true,
156
+ validate: async (value) => {
157
+ if (!/^[a-z][a-z0-9-_]+$/.test(value || "")) {
158
+ return "Invalid name";
159
+ }
160
+ const presetData = list.find((presetData) => {
161
+ return presetData.name === value;
162
+ });
163
+ if (presetData) {
164
+ return "Preset name is already taken";
165
+ }
166
+ return true;
167
+ },
168
+ default: preset.name
169
+ });
170
+ preset.version = await (0, utils_1.promptText)({
171
+ message: "Preset version:",
172
+ validate: (version) => {
173
+ if (!/^[0-9]+\.[0.9]+\.[0-9]+$/.test(version)) {
174
+ return "Invalid version";
175
+ }
176
+ return true;
177
+ },
178
+ default: preset.version
179
+ });
180
+ preset.type = await (0, utils_1.promptSelect)({
181
+ message: "Preset type:",
182
+ options: ["dockerfile", "image"]
183
+ });
184
+ switch (preset.type) {
185
+ case "dockerfile":
186
+ const files = await fs.readdirFiles();
187
+ const dockerfiles = files.filter((fileName) => {
188
+ if (new RegExp("^(.*)\\.dockerfile$").test(fileName)) {
189
+ return true;
190
+ }
191
+ return new RegExp("^Dockerfile(\\..*)?").test(fileName);
192
+ });
193
+ if (dockerfiles.length === 0) {
194
+ throw new Error("No dockerfiles found");
195
+ }
196
+ preset.dockerfile = await (0, utils_1.promptSelect)({
197
+ message: "Preset dockerfile:",
198
+ options: dockerfiles
199
+ });
200
+ break;
201
+ case "image":
202
+ preset.image = await (0, utils_1.promptText)({
203
+ message: "Preset image:",
204
+ required: true,
205
+ validate(value) {
206
+ if (!/^[a-z0-9]+(?:[._-][a-z0-9]+)*(?::[a-z0-9]+(?:[._-][a-z0-9]+)*)?$/.test(value)) {
207
+ return "Invalid image name";
208
+ }
209
+ return true;
210
+ }
211
+ });
212
+ break;
213
+ }
214
+ console.info(JSON.stringify(preset.toJSON(), null, 4));
215
+ const confirm = await (0, utils_1.promptConfirm)({
216
+ message: "Correct?"
217
+ });
218
+ if (confirm) {
219
+ await preset.save();
220
+ }
221
+ }
222
+ }
120
223
  async get(name) {
121
224
  const list = await this.getList();
122
225
  const item = list.find((item) => {
@@ -125,12 +228,6 @@ let PresetService = class PresetService {
125
228
  if (!item) {
126
229
  throw new Error(`Preset ${name} not found`);
127
230
  }
128
- if (item.source === core_1.PRESET_SOURCE_GITHUB) {
129
- item.path = this.appConfigService.dataPath("presets", item.name);
130
- }
131
- else if (item.source === core_1.PRESET_SOURCE_INTERNAL) {
132
- item.path = Path.join(env_1.PRESETS_DIR, item.name);
133
- }
134
231
  const config = await makes_1.FS.readJSON(item.path, "config.json");
135
232
  return this.toObject({
136
233
  ...item,
@@ -180,7 +277,7 @@ let PresetService = class PresetService {
180
277
  console.log(preset.version);
181
278
  }
182
279
  async search(options = {}) {
183
- const { name, source } = options;
280
+ const { name, source, path } = options;
184
281
  const presets = [];
185
282
  const presetConfigs = await this.getList();
186
283
  for (const presetConfig of presetConfigs) {
@@ -190,11 +287,11 @@ let PresetService = class PresetService {
190
287
  if (source && source !== presetConfig.source) {
191
288
  continue;
192
289
  }
290
+ if (path && path !== presetConfig.path) {
291
+ continue;
292
+ }
193
293
  try {
194
294
  const fullConfig = await makes_1.FS.readJSON(presetConfig.path, "config.json");
195
- if (!fullConfig.name) {
196
- console.log(presetConfig.name);
197
- }
198
295
  const preset = this.toObject({
199
296
  ...presetConfig,
200
297
  ...fullConfig
@@ -202,6 +299,7 @@ let PresetService = class PresetService {
202
299
  presets.push(preset);
203
300
  }
204
301
  catch (err) {
302
+ this.logService.error("PresetService.search(", options, ") ->", err.message);
205
303
  }
206
304
  }
207
305
  return presets;
@@ -214,5 +312,6 @@ let PresetService = class PresetService {
214
312
  exports.PresetService = PresetService;
215
313
  exports.PresetService = PresetService = __decorate([
216
314
  (0, core_1.Injectable)(),
217
- __metadata("design:paramtypes", [core_1.AppConfigService])
315
+ __metadata("design:paramtypes", [AppConfigService_1.AppConfigService,
316
+ LogService_1.LogService])
218
317
  ], PresetService);
@@ -1,7 +1,6 @@
1
1
  import { Project, ProjectProperties } from "@wocker/core";
2
2
  import { DockerService, AppConfigService, AppEventsService } from "../services";
3
3
  type SearchParams = Partial<{
4
- id: string;
5
4
  name: string;
6
5
  path: string;
7
6
  }>;
@@ -56,7 +56,7 @@ let ProjectService = class ProjectService {
56
56
  }
57
57
  async get() {
58
58
  const project = await this.searchOne({
59
- path: this.appConfigService.getPWD()
59
+ path: this.appConfigService.pwd()
60
60
  });
61
61
  if (!project) {
62
62
  throw new Error("Project not found");
@@ -64,8 +64,13 @@ let ProjectService = class ProjectService {
64
64
  return project;
65
65
  }
66
66
  async getById(id) {
67
+ const config = this.appConfigService.getConfig();
68
+ const projectData = config.getProject(id);
67
69
  const data = await makes_1.FS.readJSON(this.appConfigService.dataPath("projects", id, "config.json"));
68
- return this.fromObject(data);
70
+ return this.fromObject({
71
+ ...data,
72
+ path: projectData.path || projectData.src
73
+ });
69
74
  }
70
75
  async cdProject(name) {
71
76
  const project = await this.searchOne({
@@ -96,7 +101,7 @@ let ProjectService = class ProjectService {
96
101
  await this.dockerService.buildImage({
97
102
  tag: project.imageName,
98
103
  buildArgs: project.buildArgs,
99
- context: this.appConfigService.getPWD(),
104
+ context: this.appConfigService.pwd(),
100
105
  src: project.dockerfile
101
106
  });
102
107
  }
@@ -116,7 +121,10 @@ let ProjectService = class ProjectService {
116
121
  volumes: (project.volumes || []).map((volume) => {
117
122
  const regVolume = /^([^:]+):([^:]+)(?::([^:]+))?$/;
118
123
  const [, source, destination, options] = regVolume.exec(volume);
119
- return `${Path.join(this.appConfigService.getPWD(), source)}:${destination}` + (options ? `:${options}` : "");
124
+ if (source.startsWith("/")) {
125
+ return volume;
126
+ }
127
+ return `${Path.join(this.appConfigService.pwd(), source)}:${destination}` + (options ? `:${options}` : "");
120
128
  }),
121
129
  ports: project.ports || []
122
130
  });
@@ -145,27 +153,25 @@ let ProjectService = class ProjectService {
145
153
  if (!project.id) {
146
154
  project.id = project.name;
147
155
  }
148
- const projectDirPath = this.appConfigService.dataPath("projects", project.id);
149
156
  const config = await this.appConfigService.getConfig();
150
- const configPath = this.appConfigService.dataPath("projects", project.id, "config.json");
151
- if (!makes_1.FS.existsSync(projectDirPath)) {
152
- await makes_1.FS.mkdir(projectDirPath, {
153
- recursive: true
154
- });
157
+ const fs = new core_1.FileSystem(this.appConfigService.dataPath("projects", project.id));
158
+ if (!fs.exists()) {
159
+ fs.mkdir("", { recursive: true });
155
160
  }
156
- config.setProject(project.id, project.path);
157
- await makes_1.FS.writeJSON(configPath, project);
161
+ const { path, ...rest } = project.toJSON();
162
+ config.addProject(project.id, project.name, path);
163
+ await fs.writeJSON("config.json", rest);
158
164
  await config.save();
159
165
  }
160
166
  async search(params = {}) {
161
- const { id, name, path } = params;
167
+ const { name, path } = params;
162
168
  const config = await this.appConfigService.getConfig();
163
169
  const projects = [];
164
170
  for (const projectConfig of config.projects) {
165
- if (id && projectConfig.id !== id) {
171
+ if (name && projectConfig.name !== name) {
166
172
  continue;
167
173
  }
168
- if (path && projectConfig.src !== path) {
174
+ if (path && (projectConfig.path || projectConfig.src) !== path) {
169
175
  continue;
170
176
  }
171
177
  const project = await this.getById(projectConfig.id);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wocker/ws",
3
- "version": "1.0.15",
3
+ "version": "1.0.16",
4
4
  "author": "Kris Papercut <krispcut@gmail.com>",
5
5
  "description": "Docker workspace for web projects",
6
6
  "license": "MIT",
@@ -25,7 +25,7 @@
25
25
  "lint": "eslint \"**/*.{js,jsx,ts,tsx}\""
26
26
  },
27
27
  "dependencies": {
28
- "@wocker/core": "^1.0.14",
28
+ "@wocker/core": "1.0.16",
29
29
  "@wocker/utils": "^1.0.5",
30
30
  "async-mutex": "^0.4.0",
31
31
  "axios": "^1.6.7",