harmonyc 0.23.2 → 0.24.0

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.
@@ -4,7 +4,9 @@ export declare class PhrasesAssistant {
4
4
  project: t.Project;
5
5
  file: t.SourceFile;
6
6
  clazz: t.ClassDeclaration;
7
- constructor(content: string, className: string);
7
+ existingMethods: Set<string>;
8
+ constructor(project: t.Project, path: string, className: string);
9
+ discoverMethods(clazz: t.ClassDeclaration): void;
8
10
  ensureMethods(methods: PhraseMethod[]): void;
9
11
  ensureMethod(method: PhraseMethod): void;
10
12
  addMethod(method: PhraseMethod): void;
@@ -1,13 +1,11 @@
1
1
  import * as t from 'ts-morph';
2
2
  export class PhrasesAssistant {
3
- constructor(content, className) {
3
+ constructor(project, path, className) {
4
4
  var _a;
5
- this.project = new t.Project({
6
- useInMemoryFileSystem: true,
7
- });
8
- this.file = this.project.createSourceFile('filename.ts', content, {
9
- overwrite: true,
10
- });
5
+ this.existingMethods = new Set();
6
+ this.project = project;
7
+ this.file = this.project.addSourceFileAtPath(path);
8
+ this.file.refreshFromFileSystemSync();
11
9
  const clazz = this.file.getClass(className);
12
10
  if (!clazz) {
13
11
  const defaultExport = this.file.getDefaultExportSymbol();
@@ -29,6 +27,16 @@ export class PhrasesAssistant {
29
27
  name: className,
30
28
  isDefaultExport: true,
31
29
  }));
30
+ this.discoverMethods(this.clazz);
31
+ }
32
+ discoverMethods(clazz) {
33
+ for (const method of clazz.getMethods()) {
34
+ this.existingMethods.add(method.getName());
35
+ }
36
+ const baseClass = clazz.getBaseClass();
37
+ if (baseClass) {
38
+ this.discoverMethods(baseClass);
39
+ }
32
40
  }
33
41
  ensureMethods(methods) {
34
42
  for (const method of methods) {
@@ -49,8 +57,7 @@ export class PhrasesAssistant {
49
57
  this.sortMethods();
50
58
  }
51
59
  ensureMethod(method) {
52
- let existing = this.clazz.getMethod(method.name);
53
- if (!existing) {
60
+ if (!this.existingMethods.has(method.name)) {
54
61
  this.addMethod(method);
55
62
  }
56
63
  }
@@ -1,5 +1,6 @@
1
1
  import { readFile, writeFile } from 'fs/promises';
2
2
  import c from 'tinyrainbow';
3
+ import { Project } from 'ts-morph';
3
4
  import { compileFeature } from "../compiler/compile.js";
4
5
  import { preprocess } from "../compiler/compiler.js";
5
6
  import { PhrasesAssistant } from "../phrases_assistant/phrases_assistant.js";
@@ -8,6 +9,7 @@ const DEFAULT_OPTIONS = {
8
9
  };
9
10
  export default function harmonyPlugin(opts = {}) {
10
11
  const options = { ...DEFAULT_OPTIONS, ...opts };
12
+ const project = new Project();
11
13
  return {
12
14
  name: 'harmony',
13
15
  resolveId(id) {
@@ -21,7 +23,7 @@ export default function harmonyPlugin(opts = {}) {
21
23
  code = preprocess(code);
22
24
  const { outFile, phraseMethods, featureClassName } = compileFeature(id, code, opts);
23
25
  if (options.autoEditPhrases) {
24
- void updatePhrasesFile(id, phraseMethods, featureClassName);
26
+ void updatePhrasesFile(project, id, phraseMethods, featureClassName);
25
27
  }
26
28
  return {
27
29
  code: outFile.valueWithoutSourceMap,
@@ -81,7 +83,7 @@ function addPhrases(task, depth = 2) {
81
83
  delete task.meta.phrases; // to make sure not to add them again
82
84
  }
83
85
  }
84
- async function updatePhrasesFile(id, phraseMethods, featureClassName) {
86
+ async function updatePhrasesFile(project, id, phraseMethods, featureClassName) {
85
87
  try {
86
88
  const phrasesFile = id.replace(/\.harmony$/, '.phrases.ts');
87
89
  let phrasesFileContent = '';
@@ -91,9 +93,13 @@ async function updatePhrasesFile(id, phraseMethods, featureClassName) {
91
93
  catch {
92
94
  // File doesn't exist
93
95
  }
94
- const pa = new PhrasesAssistant(phrasesFileContent, featureClassName);
96
+ const pa = new PhrasesAssistant(project, phrasesFile, featureClassName);
95
97
  pa.ensureMethods(phraseMethods);
96
- await writeFile(phrasesFile, pa.toCode());
98
+ const newContent = pa.toCode();
99
+ if (newContent === phrasesFileContent) {
100
+ return; // No changes needed
101
+ }
102
+ await writeFile(phrasesFile, newContent, 'utf-8');
97
103
  }
98
104
  catch (e) {
99
105
  console.error('Error updating phrases file:', e);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "harmonyc",
3
3
  "description": "Harmony Code - model-driven BDD for Vitest",
4
- "version": "0.23.2",
4
+ "version": "0.24.0",
5
5
  "author": "Bernát Kalló",
6
6
  "homepage": "https://github.com/harmony-ac/code#readme",
7
7
  "repository": {