screw-up 1.3.0 → 1.5.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.
@@ -1,11 +1,11 @@
1
1
  /*!
2
2
  * name: screw-up
3
- * version: 1.3.0
3
+ * version: 1.5.0
4
4
  * description: Simply package metadata inserter on Vite plugin
5
5
  * author: Kouji Matsui (@kekyo@mi.kekyo.net)
6
6
  * license: MIT
7
7
  * repository.url: https://github.com/kekyo/screw-up.git
8
- * git.commit.hash: eaf2f91941a8c1d085b1c493a55e0041b39f0cb3
8
+ * git.commit.hash: 72fd5b88656bc400ba3fed500895826208e23217
9
9
  */
10
10
  import { existsSync } from "fs";
11
11
  import * as fs from "fs/promises";
@@ -1120,8 +1120,7 @@ const createConsoleLogger = (prefix, driver) => {
1120
1120
  };
1121
1121
  const flattenObject = (obj, prefix, map) => {
1122
1122
  for (const [key2, value] of Object.entries(obj)) {
1123
- if (!value)
1124
- continue;
1123
+ if (!value) continue;
1125
1124
  const fullKey = prefix ? `${prefix}.${key2}` : key2;
1126
1125
  if (typeof value === "string") {
1127
1126
  map[fullKey] = value;
@@ -1146,7 +1145,9 @@ const findWorkspaceRoot = async (startPath, logger) => {
1146
1145
  return currentPath;
1147
1146
  }
1148
1147
  } catch (error) {
1149
- logger.warn(`Failed to parse package.json at ${packageJsonPath}: ${error}`);
1148
+ logger.warn(
1149
+ `Failed to parse package.json at ${packageJsonPath}: ${error}`
1150
+ );
1150
1151
  }
1151
1152
  }
1152
1153
  currentPath = dirname(currentPath);
@@ -1191,12 +1192,16 @@ const collectWorkspaceSiblings = async (workspaceRoot, fetchGitMetadata, alwaysO
1191
1192
  });
1192
1193
  }
1193
1194
  } catch (error) {
1194
- logger.warn(`Failed to resolve package.json from ${packageJsonPath}: ${error}`);
1195
+ logger.warn(
1196
+ `Failed to resolve package.json from ${packageJsonPath}: ${error}`
1197
+ );
1195
1198
  }
1196
1199
  }
1197
1200
  }
1198
1201
  } catch (error) {
1199
- logger.warn(`Failed to collect workspace siblings from ${workspaceRoot}: ${error}`);
1202
+ logger.warn(
1203
+ `Failed to collect workspace siblings from ${workspaceRoot}: ${error}`
1204
+ );
1200
1205
  }
1201
1206
  return siblings;
1202
1207
  };
@@ -1205,7 +1210,9 @@ const replacePeerDependenciesWildcards = (packageJson, siblings, versionPrefix)
1205
1210
  if (!modifiedPackageJson.peerDependencies || typeof modifiedPackageJson.peerDependencies !== "object") {
1206
1211
  return modifiedPackageJson;
1207
1212
  }
1208
- for (const [depName, depVersion] of Object.entries(modifiedPackageJson.peerDependencies)) {
1213
+ for (const [depName, depVersion] of Object.entries(
1214
+ modifiedPackageJson.peerDependencies
1215
+ )) {
1209
1216
  if (depVersion === "*" && siblings.has(depName)) {
1210
1217
  const sibling = siblings.get(depName);
1211
1218
  modifiedPackageJson.peerDependencies[depName] = `${versionPrefix}${sibling.version}`;
@@ -1316,7 +1323,12 @@ const resolvePackageMetadata = async (projectRoot, fetchGitMetadata, alwaysOverr
1316
1323
  projectRoot,
1317
1324
  logger,
1318
1325
  readPackageMetadata.bind(void 0, logger),
1319
- mergePackageMetadata.bind(void 0, fetchGitMetadata, alwaysOverrideVersionFromGit, sourceMap)
1326
+ mergePackageMetadata.bind(
1327
+ void 0,
1328
+ fetchGitMetadata,
1329
+ alwaysOverrideVersionFromGit,
1330
+ sourceMap
1331
+ )
1320
1332
  );
1321
1333
  return {
1322
1334
  metadata,
@@ -1338,7 +1350,13 @@ const resolveRawPackageJsonObject = async (projectRoot, fetchGitMetadata, always
1338
1350
  projectRoot,
1339
1351
  logger,
1340
1352
  readRawPackageJson.bind(void 0, logger),
1341
- mergeRawPackageJson.bind(void 0, fetchGitMetadata, alwaysOverrideVersionFromGit, inheritableFields, sourceMap)
1353
+ mergeRawPackageJson.bind(
1354
+ void 0,
1355
+ fetchGitMetadata,
1356
+ alwaysOverrideVersionFromGit,
1357
+ inheritableFields,
1358
+ sourceMap
1359
+ )
1342
1360
  );
1343
1361
  return {
1344
1362
  metadata: packageJson,
@@ -1737,8 +1755,16 @@ const getCommit = async (repositoryPath, hash) => {
1737
1755
  };
1738
1756
  const getCurrentCommit = async (repositoryPath) => {
1739
1757
  try {
1740
- const currentOid = await git.resolveRef({ fs, dir: repositoryPath, ref: "HEAD" });
1741
- const commit = await git.readCommit({ fs, dir: repositoryPath, oid: currentOid });
1758
+ const currentOid = await git.resolveRef({
1759
+ fs,
1760
+ dir: repositoryPath,
1761
+ ref: "HEAD"
1762
+ });
1763
+ const commit = await git.readCommit({
1764
+ fs,
1765
+ dir: repositoryPath,
1766
+ oid: currentOid
1767
+ });
1742
1768
  return {
1743
1769
  hash: commit.oid,
1744
1770
  shortHash: commit.oid.substring(0, 7),
@@ -1750,44 +1776,72 @@ const getCurrentCommit = async (repositoryPath) => {
1750
1776
  return void 0;
1751
1777
  }
1752
1778
  };
1753
- const getRelatedTags = async (repositoryPath, commitHash) => {
1779
+ const buildTagCache = async (repositoryPath) => {
1780
+ const cache = {
1781
+ commitToTags: /* @__PURE__ */ new Map(),
1782
+ initialized: true
1783
+ };
1754
1784
  try {
1755
1785
  const tags = await git.listTags({ fs, dir: repositoryPath });
1756
- const tagInfos = [];
1757
- for (const tagName of tags) {
1758
- try {
1759
- const tagOid = await git.resolveRef({ fs, dir: repositoryPath, ref: `refs/tags/${tagName}` });
1760
- let targetCommitOid = tagOid;
1786
+ await Promise.all(
1787
+ tags.map(async (tagName) => {
1761
1788
  try {
1762
- const tagObject = await git.readTag({ fs, dir: repositoryPath, oid: tagOid });
1763
- if (tagObject && tagObject.tag.object) {
1764
- targetCommitOid = tagObject.tag.object;
1765
- }
1766
- } catch (e) {
1767
- }
1768
- if (targetCommitOid === commitHash) {
1789
+ const tagOid = await git.resolveRef({
1790
+ fs,
1791
+ dir: repositoryPath,
1792
+ ref: `refs/tags/${tagName}`
1793
+ });
1769
1794
  const version2 = parseVersion(tagName);
1770
- if (version2 && isValidVersion(version2)) {
1771
- tagInfos.push({
1772
- name: tagName,
1773
- hash: commitHash,
1774
- version: version2
1775
- });
1776
- } else {
1777
- tagInfos.push({
1778
- name: tagName,
1779
- hash: commitHash,
1780
- version: void 0
1795
+ const tagInfo = {
1796
+ name: tagName,
1797
+ hash: tagOid,
1798
+ // This will be updated for annotated tags
1799
+ version: version2 && isValidVersion(version2) ? version2 : void 0
1800
+ };
1801
+ if (!cache.commitToTags.has(tagOid)) {
1802
+ cache.commitToTags.set(tagOid, []);
1803
+ }
1804
+ cache.commitToTags.get(tagOid).push({ ...tagInfo, hash: tagOid });
1805
+ try {
1806
+ const tagObject = await git.readTag({
1807
+ fs,
1808
+ dir: repositoryPath,
1809
+ oid: tagOid
1781
1810
  });
1811
+ if (tagObject && tagObject.tag.object) {
1812
+ const commitOid = tagObject.tag.object;
1813
+ const lightweightTags = cache.commitToTags.get(tagOid);
1814
+ if (lightweightTags) {
1815
+ const index = lightweightTags.findIndex(
1816
+ (t) => t.name === tagName
1817
+ );
1818
+ if (index >= 0) {
1819
+ lightweightTags.splice(index, 1);
1820
+ if (lightweightTags.length === 0) {
1821
+ cache.commitToTags.delete(tagOid);
1822
+ }
1823
+ }
1824
+ }
1825
+ if (!cache.commitToTags.has(commitOid)) {
1826
+ cache.commitToTags.set(commitOid, []);
1827
+ }
1828
+ cache.commitToTags.get(commitOid).push({ ...tagInfo, hash: commitOid });
1829
+ }
1830
+ } catch (e) {
1782
1831
  }
1832
+ } catch (e) {
1783
1833
  }
1784
- } catch (e) {
1785
- }
1786
- }
1787
- return tagInfos.sort((a, b) => a.name.localeCompare(b.name));
1834
+ })
1835
+ );
1788
1836
  } catch (e) {
1837
+ }
1838
+ return cache;
1839
+ };
1840
+ const getRelatedTagsFromCache = (cache, commitHash) => {
1841
+ if (!cache.initialized) {
1789
1842
  return [];
1790
1843
  }
1844
+ return cache.commitToTags.get(commitHash) || [];
1791
1845
  };
1792
1846
  const getRelatedTagsForVersioning = async (repositoryPath, commitHash) => {
1793
1847
  try {
@@ -1795,16 +1849,12 @@ const getRelatedTagsForVersioning = async (repositoryPath, commitHash) => {
1795
1849
  const tagInfos = [];
1796
1850
  for (const tagName of tags) {
1797
1851
  try {
1798
- const tagOid = await git.resolveRef({ fs, dir: repositoryPath, ref: `refs/tags/${tagName}` });
1799
- let targetCommitOid = tagOid;
1800
- try {
1801
- const tagObject = await git.readTag({ fs, dir: repositoryPath, oid: tagOid });
1802
- if (tagObject && tagObject.tag.object) {
1803
- targetCommitOid = tagObject.tag.object;
1804
- }
1805
- } catch (e) {
1806
- }
1807
- if (targetCommitOid === commitHash) {
1852
+ const tagOid = await git.resolveRef({
1853
+ fs,
1854
+ dir: repositoryPath,
1855
+ ref: `refs/tags/${tagName}`
1856
+ });
1857
+ if (tagOid === commitHash) {
1808
1858
  const version2 = parseVersion(tagName);
1809
1859
  if (version2 && isValidVersion(version2)) {
1810
1860
  tagInfos.push({
@@ -1813,6 +1863,25 @@ const getRelatedTagsForVersioning = async (repositoryPath, commitHash) => {
1813
1863
  version: version2
1814
1864
  });
1815
1865
  }
1866
+ } else {
1867
+ try {
1868
+ const tagObject = await git.readTag({
1869
+ fs,
1870
+ dir: repositoryPath,
1871
+ oid: tagOid
1872
+ });
1873
+ if (tagObject && tagObject.tag.object === commitHash) {
1874
+ const version2 = parseVersion(tagName);
1875
+ if (version2 && isValidVersion(version2)) {
1876
+ tagInfos.push({
1877
+ name: tagName,
1878
+ hash: commitHash,
1879
+ version: version2
1880
+ });
1881
+ }
1882
+ }
1883
+ } catch (e) {
1884
+ }
1816
1885
  }
1817
1886
  } catch (e) {
1818
1887
  }
@@ -1828,7 +1897,11 @@ const getRelatedBranches = async (repositoryPath, commitHash) => {
1828
1897
  const relatedBranches = [];
1829
1898
  for (const branch of branches) {
1830
1899
  try {
1831
- const branchOid = await git.resolveRef({ fs, dir: repositoryPath, ref: branch });
1900
+ const branchOid = await git.resolveRef({
1901
+ fs,
1902
+ dir: repositoryPath,
1903
+ ref: branch
1904
+ });
1832
1905
  if (branchOid === commitHash) {
1833
1906
  relatedBranches.push(branch);
1834
1907
  }
@@ -1845,7 +1918,7 @@ const getModifiedFiles = async (repositoryPath) => {
1845
1918
  const status = await git.statusMatrix({ fs, dir: repositoryPath });
1846
1919
  return status.filter(
1847
1920
  ([, head, workdir, stage]) => workdir === 2 || // modified in working directory (unstaged)
1848
- stage === 2 || // modified in stage (staged)
1921
+ stage === 2 || // modified in stage (staged)
1849
1922
  stage === 3 || // added to stage (staged)
1850
1923
  head === 1 && workdir === 0 || // deleted from working directory
1851
1924
  head === 0 && workdir === 1
@@ -1858,7 +1931,7 @@ const getModifiedFiles = async (repositoryPath) => {
1858
1931
  const formatModifiedFile = (modifiedFile) => {
1859
1932
  return `'${modifiedFile[0]}':${modifiedFile[1]}:${modifiedFile[2]}:${modifiedFile[3]}`;
1860
1933
  };
1861
- const lookupVersionLabelRecursive = async (cwd, commit, reachedCommits) => {
1934
+ const lookupVersionLabelRecursive = async (cwd, commit, reachedCommits, tagCache) => {
1862
1935
  const scheduledStack = [];
1863
1936
  let version2 = { major: 0, minor: 0, build: 1, original: "0.0.1" };
1864
1937
  let currentCommit = commit;
@@ -1867,7 +1940,7 @@ const lookupVersionLabelRecursive = async (cwd, commit, reachedCommits) => {
1867
1940
  version2 = reachedCommits.get(currentCommit.hash);
1868
1941
  break;
1869
1942
  }
1870
- const relatedTags = await getRelatedTagsForVersioning(cwd, currentCommit.hash);
1943
+ const relatedTags = tagCache ? getRelatedTagsFromCache(tagCache, currentCommit.hash) : await getRelatedTagsForVersioning(cwd, currentCommit.hash);
1871
1944
  const versionCandidates = relatedTags.filter((tag) => tag.version && isValidVersion(tag.version)).filter((tag) => tag.version.minor !== void 0).sort((a, b) => compareVersions(a.version, b.version));
1872
1945
  if (versionCandidates.length >= 1) {
1873
1946
  version2 = versionCandidates[0].version;
@@ -1876,9 +1949,15 @@ const lookupVersionLabelRecursive = async (cwd, commit, reachedCommits) => {
1876
1949
  }
1877
1950
  let parentCommits = [];
1878
1951
  try {
1879
- const commitObj = await git.readCommit({ fs, dir: cwd, oid: currentCommit.hash });
1952
+ const commitObj = await git.readCommit({
1953
+ fs,
1954
+ dir: cwd,
1955
+ oid: currentCommit.hash
1956
+ });
1880
1957
  const parentHashes = commitObj.commit.parent || [];
1881
- parentCommits = (await Promise.all(parentHashes.map((parentHash) => getCommit(cwd, parentHash)))).filter((ci) => ci !== void 0);
1958
+ parentCommits = (await Promise.all(
1959
+ parentHashes.map((parentHash) => getCommit(cwd, parentHash))
1960
+ )).filter((ci) => ci !== void 0);
1882
1961
  } catch (e) {
1883
1962
  }
1884
1963
  if (parentCommits.length === 0) {
@@ -1893,7 +1972,12 @@ const lookupVersionLabelRecursive = async (cwd, commit, reachedCommits) => {
1893
1972
  const { commit: scheduledCommit, parents } = scheduled;
1894
1973
  if (parents.length >= 2) {
1895
1974
  for (let index = 1; index < parents.length; index++) {
1896
- const alternateParentVersion = await lookupVersionLabelRecursive(cwd, parents[index], reachedCommits);
1975
+ const alternateParentVersion = await lookupVersionLabelRecursive(
1976
+ cwd,
1977
+ parents[index],
1978
+ reachedCommits,
1979
+ tagCache
1980
+ );
1897
1981
  if (alternateParentVersion && compareVersions(alternateParentVersion, version2) < 0) {
1898
1982
  version2 = alternateParentVersion;
1899
1983
  }
@@ -1917,8 +2001,17 @@ const getGitMetadata = async (repositoryPath, checkWorkingDirectoryStatus, logge
1917
2001
  if (!currentCommit) {
1918
2002
  return metadata;
1919
2003
  }
2004
+ const tagCache = await buildTagCache(gitRootPath);
2005
+ logger.debug(
2006
+ `Built tag cache with ${tagCache.commitToTags.size} commit entries`
2007
+ );
1920
2008
  const reachedCommits = /* @__PURE__ */ new Map();
1921
- let version2 = await lookupVersionLabelRecursive(gitRootPath, currentCommit, reachedCommits);
2009
+ let version2 = await lookupVersionLabelRecursive(
2010
+ gitRootPath,
2011
+ currentCommit,
2012
+ reachedCommits,
2013
+ tagCache
2014
+ );
1922
2015
  const gitMetadata = { tags: [], branches: [] };
1923
2016
  metadata.git = gitMetadata;
1924
2017
  if (version2) {
@@ -1926,7 +2019,9 @@ const getGitMetadata = async (repositoryPath, checkWorkingDirectoryStatus, logge
1926
2019
  const modifiedFiles = await getModifiedFiles(gitRootPath);
1927
2020
  if (modifiedFiles.length >= 1) {
1928
2021
  const newVersion = incrementLastVersionComponent(version2);
1929
- logger.debug(`Increased git version by detected modified items: ${formatVersion(version2)} ---> ${formatVersion(newVersion)}, Files=[${modifiedFiles.map(formatModifiedFile).join(", ")}]`);
2022
+ logger.debug(
2023
+ `Increased git version by detected modified items: ${formatVersion(version2)} ---> ${formatVersion(newVersion)}, Files=[${modifiedFiles.map(formatModifiedFile).join(", ")}]`
2024
+ );
1930
2025
  version2 = newVersion;
1931
2026
  }
1932
2027
  }
@@ -1940,9 +2035,12 @@ const getGitMetadata = async (repositoryPath, checkWorkingDirectoryStatus, logge
1940
2035
  date: dayjs(currentCommit.date).format("YYYY-MM-DDTHH:mm:ssZ[Z]"),
1941
2036
  message: currentCommit.message
1942
2037
  };
1943
- const relatedTags = await getRelatedTags(gitRootPath, currentCommit.hash);
2038
+ const relatedTags = getRelatedTagsFromCache(tagCache, currentCommit.hash);
1944
2039
  gitMetadata.tags = relatedTags.map((tag) => tag.name);
1945
- const relatedBranches = await getRelatedBranches(gitRootPath, currentCommit.hash);
2040
+ const relatedBranches = await getRelatedBranches(
2041
+ gitRootPath,
2042
+ currentCommit.hash
2043
+ );
1946
2044
  gitMetadata.branches = relatedBranches;
1947
2045
  } catch (error) {
1948
2046
  logger.warn(`Failed to extract git metadata: ${error}`);
@@ -1963,11 +2061,11 @@ const getFetchGitMetadata = (targetDir, checkWorkingDirectoryStatus, logger) =>
1963
2061
  };
1964
2062
  };
1965
2063
  const name = "screw-up";
1966
- const version = "1.3.0";
2064
+ const version = "1.5.0";
1967
2065
  const author = "Kouji Matsui (@kekyo@mi.kekyo.net)";
1968
2066
  const license = "MIT";
1969
2067
  const repository_url = "https://github.com/kekyo/screw-up.git";
1970
- const git_commit_hash = "eaf2f91941a8c1d085b1c493a55e0041b39f0cb3";
2068
+ const git_commit_hash = "72fd5b88656bc400ba3fed500895826208e23217";
1971
2069
  const packageMetadata = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
1972
2070
  __proto__: null,
1973
2071
  author,
@@ -1990,4 +2088,4 @@ export {
1990
2088
  resolvePackageMetadata as r,
1991
2089
  version as v
1992
2090
  };
1993
- //# sourceMappingURL=packageMetadata-CFcZlT3V.js.map
2091
+ //# sourceMappingURL=packageMetadata-C2Yob7-F.js.map