@stackbit/cms-core 0.1.31 → 0.2.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.
- package/dist/content-store-utils.d.ts.map +1 -1
- package/dist/content-store-utils.js +9 -1
- package/dist/content-store-utils.js.map +1 -1
- package/dist/content-store.d.ts +8 -0
- package/dist/content-store.d.ts.map +1 -1
- package/dist/content-store.js +9 -4
- package/dist/content-store.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/services/git.d.ts +38 -0
- package/dist/services/git.d.ts.map +1 -0
- package/dist/services/git.js +201 -0
- package/dist/services/git.js.map +1 -0
- package/dist/services/index.d.ts +3 -0
- package/dist/services/index.d.ts.map +1 -0
- package/dist/services/index.js +15 -0
- package/dist/services/index.js.map +1 -0
- package/dist/services/run.d.ts +7 -0
- package/dist/services/run.d.ts.map +1 -0
- package/dist/services/run.js +53 -0
- package/dist/services/run.js.map +1 -0
- package/dist/utils/create-update-csi-docs.d.ts.map +1 -1
- package/dist/utils/create-update-csi-docs.js +18 -8
- package/dist/utils/create-update-csi-docs.js.map +1 -1
- package/dist/utils/duplicate-document.js +4 -0
- package/dist/utils/duplicate-document.js.map +1 -1
- package/dist/utils/index.d.ts +2 -2
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/store-to-api-docs-converter.d.ts +1 -1
- package/dist/utils/store-to-api-docs-converter.d.ts.map +1 -1
- package/dist/utils/store-to-api-docs-converter.js +50 -28
- package/dist/utils/store-to-api-docs-converter.js.map +1 -1
- package/package.json +9 -6
- package/src/content-store-utils.ts +9 -1
- package/src/content-store.ts +18 -5
- package/src/index.ts +1 -0
- package/src/services/git.ts +245 -0
- package/src/services/index.ts +2 -0
- package/src/services/run.ts +54 -0
- package/src/utils/create-update-csi-docs.ts +19 -7
- package/src/utils/duplicate-document.ts +3 -0
- package/src/utils/store-to-api-docs-converter.ts +47 -27
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.mapStoreAssetsToAPIAssets = exports.mapAssetsToLocalizedApiImages = exports.mapDocumentsToLocalizedApiObjects = void 0;
|
|
7
7
|
const lodash_1 = __importDefault(require("lodash"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
8
9
|
const utils_1 = require("@stackbit/utils");
|
|
9
10
|
function mapDocumentsToLocalizedApiObjects(documents, locale) {
|
|
10
11
|
return documents.map((document) => documentToLocalizedApiObject(document, locale));
|
|
@@ -94,33 +95,46 @@ function toLocalizedAPIField(docField, locale, isListItem = false) {
|
|
|
94
95
|
};
|
|
95
96
|
}
|
|
96
97
|
case 'image':
|
|
98
|
+
let result;
|
|
97
99
|
if (docField.localized) {
|
|
98
100
|
const { localized, locales, ...base } = docField;
|
|
99
101
|
const localeProps = locales && locale ? locales[locale] : undefined;
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
102
|
+
if (localeProps) {
|
|
103
|
+
const fields = toLocalizedAPIFields(localeProps.fields, locale);
|
|
104
|
+
result = {
|
|
105
|
+
...base,
|
|
106
|
+
...localeProps,
|
|
107
|
+
fields,
|
|
108
|
+
...localeFields(localized)
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
result = {
|
|
113
|
+
...base,
|
|
114
|
+
isUnset: true,
|
|
115
|
+
...localeFields(localized)
|
|
116
|
+
};
|
|
117
|
+
}
|
|
110
118
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
119
|
+
else {
|
|
120
|
+
if (docField.isUnset) {
|
|
121
|
+
result = {
|
|
114
122
|
...docField,
|
|
115
123
|
type: 'image',
|
|
116
|
-
|
|
117
|
-
}
|
|
118
|
-
|
|
124
|
+
...localeFields(docField.localized)
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
const fields = toLocalizedAPIFields(docField.fields, locale);
|
|
129
|
+
result = {
|
|
119
130
|
...docField,
|
|
120
|
-
type: 'image'
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
131
|
+
type: 'image',
|
|
132
|
+
fields,
|
|
133
|
+
...localeFields(docField.localized)
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return result;
|
|
124
138
|
case 'object':
|
|
125
139
|
case 'model':
|
|
126
140
|
if (docField.localized) {
|
|
@@ -315,12 +329,12 @@ function localizeAssetFields(assetFields, locale) {
|
|
|
315
329
|
}
|
|
316
330
|
return fields;
|
|
317
331
|
}
|
|
318
|
-
function mapStoreAssetsToAPIAssets(assets, locale) {
|
|
319
|
-
return assets.map((asset) => storeAssetToAPIAsset(asset, locale)).filter((asset) => !!asset);
|
|
332
|
+
function mapStoreAssetsToAPIAssets(assets, staticAssetsPublicPath, locale) {
|
|
333
|
+
return assets.map((asset) => storeAssetToAPIAsset(asset, staticAssetsPublicPath, locale)).filter((asset) => !!asset);
|
|
320
334
|
}
|
|
321
335
|
exports.mapStoreAssetsToAPIAssets = mapStoreAssetsToAPIAssets;
|
|
322
|
-
function storeAssetToAPIAsset(asset, locale) {
|
|
323
|
-
var _a, _b, _c, _d, _e;
|
|
336
|
+
function storeAssetToAPIAsset(asset, staticAssetsPublicPath, locale) {
|
|
337
|
+
var _a, _b, _c, _d, _e, _f;
|
|
324
338
|
const assetTitleField = asset.fields.title;
|
|
325
339
|
const localizedTitleField = assetTitleField.localized ? (_a = assetTitleField.locales) === null || _a === void 0 ? void 0 : _a[locale] : assetTitleField;
|
|
326
340
|
const assetFileField = asset.fields.file;
|
|
@@ -331,15 +345,23 @@ function storeAssetToAPIAsset(asset, locale) {
|
|
|
331
345
|
return {
|
|
332
346
|
objectId: asset.srcObjectId,
|
|
333
347
|
createdAt: asset.createdAt,
|
|
334
|
-
url: localizedFileField.url,
|
|
348
|
+
url: (_c = replaceAssetUrlIfNeeded(staticAssetsPublicPath, localizedFileField.url)) !== null && _c !== void 0 ? _c : staticAssetsPublicPath,
|
|
335
349
|
...(0, utils_1.omitByNil)({
|
|
336
|
-
title: (
|
|
350
|
+
title: (_d = localizedTitleField === null || localizedTitleField === void 0 ? void 0 : localizedTitleField.value) !== null && _d !== void 0 ? _d : undefined,
|
|
337
351
|
fileName: localizedFileField.fileName,
|
|
338
352
|
contentType: localizedFileField.contentType,
|
|
339
353
|
size: localizedFileField.size,
|
|
340
|
-
width: (
|
|
341
|
-
height: (
|
|
354
|
+
width: (_e = localizedFileField.dimensions) === null || _e === void 0 ? void 0 : _e.width,
|
|
355
|
+
height: (_f = localizedFileField.dimensions) === null || _f === void 0 ? void 0 : _f.height
|
|
342
356
|
})
|
|
343
357
|
};
|
|
344
358
|
}
|
|
359
|
+
function replaceAssetUrlIfNeeded(staticAssetsPublicPath, value) {
|
|
360
|
+
let url = value;
|
|
361
|
+
let normalizedUrl = url === null || url === void 0 ? void 0 : url.toLowerCase();
|
|
362
|
+
if (normalizedUrl && !normalizedUrl.startsWith('http:') && !normalizedUrl.startsWith('https:') && !normalizedUrl.startsWith('//')) {
|
|
363
|
+
url = path_1.default.join(staticAssetsPublicPath, url);
|
|
364
|
+
}
|
|
365
|
+
return url;
|
|
366
|
+
}
|
|
345
367
|
//# sourceMappingURL=store-to-api-docs-converter.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"store-to-api-docs-converter.js","sourceRoot":"","sources":["../../src/utils/store-to-api-docs-converter.ts"],"names":[],"mappings":";;;;;;AAAA,oDAAuB;AACvB,2CAA4C;AAI5C,SAAgB,iCAAiC,CAAC,SAAuC,EAAE,MAAe;IACtG,OAAO,SAAS,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,4BAA4B,CAAC,QAAQ,EAA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AC,QAAQ;YACrC,WAAW,EAAE,kBAAkB,CAAC,WAAW;YAC3C,IAAI,EAAE,kBAAkB,CAAC,IAAI;YAC7B,KAAK,EAAE,MAAA,kBAAkB,CAAC,UAAU,0CAAE,KAAK;YAC3C,MAAM,EAAE,MAAA,kBAAkB,CAAC,UAAU,0CAAE,MAAM;SAChD,CAAC;KACL,CAAC;AACN,CAAC;AAED,SAAS,uBAAuB,CAAC,sBAA8B,EAAE,KAAyB;IACtF,IAAI,GAAG,GAAG,KAAK,CAAC;IAChB,IAAI,aAAa,GAAG,GAAG,aAAH,GAAG,uBAAH,GAAG,CAAE,WAAW,EAAE,CAAC;IACvC,IAAI,aAAa,IAAI,CAAC,aAAa,CAAC,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,aAAa,CAAC,UAAU,CAAC,QAAQ,CAAC,IAAI,CAAC,aAAa,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE;QAC/H,GAAG,GAAG,cAAI,CAAC,IAAI,CAAC,sBAAsB,EAAE,GAAI,CAAC,CAAC;KACjD;IACD,OAAO,GAAG,CAAC;AACf,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stackbit/cms-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "stackbit-dev",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -18,8 +18,10 @@
|
|
|
18
18
|
"author": "Stackbit Inc.",
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"devDependencies": {
|
|
21
|
+
"@types/fs-extra": "^11.0.1",
|
|
21
22
|
"@types/jest": "^27.5.2",
|
|
22
23
|
"@types/lodash": "^4.14.182",
|
|
24
|
+
"@types/uuid": "^9.0.0",
|
|
23
25
|
"jest": "^27.4.7",
|
|
24
26
|
"prettier": "^2.5.1",
|
|
25
27
|
"ts-jest": "^27.1.3",
|
|
@@ -29,9 +31,9 @@
|
|
|
29
31
|
"@babel/parser": "^7.11.5",
|
|
30
32
|
"@babel/traverse": "^7.11.5",
|
|
31
33
|
"@iarna/toml": "^2.2.3",
|
|
32
|
-
"@stackbit/sdk": "0.3.
|
|
33
|
-
"@stackbit/types": "0.
|
|
34
|
-
"@stackbit/utils": "0.2.
|
|
34
|
+
"@stackbit/sdk": "0.3.25",
|
|
35
|
+
"@stackbit/types": "0.3.0",
|
|
36
|
+
"@stackbit/utils": "0.2.22",
|
|
35
37
|
"chalk": "^4.0.1",
|
|
36
38
|
"esm": "^3.2.25",
|
|
37
39
|
"fs-extra": "^8.1.0",
|
|
@@ -43,7 +45,8 @@
|
|
|
43
45
|
"moment": "^2.29.1",
|
|
44
46
|
"parse5": "^6.0.1",
|
|
45
47
|
"sanitize-filename": "^1.6.3",
|
|
46
|
-
"slugify": "^1.6.5"
|
|
48
|
+
"slugify": "^1.6.5",
|
|
49
|
+
"uuid": "^9.0.0"
|
|
47
50
|
},
|
|
48
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "a04ff1f665119bd1e9d2e6603a36288b1f944dbe"
|
|
49
52
|
}
|
|
@@ -14,7 +14,15 @@ export function getContentSourceId(contentSourceType: string, srcProjectId: stri
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
export function getUserContextForSrcType(srcType: string, user?: ContentStoreTypes.User): unknown {
|
|
17
|
-
|
|
17
|
+
if (!user) {
|
|
18
|
+
return user;
|
|
19
|
+
}
|
|
20
|
+
const connection = user?.connections?.find((connection) => connection.type === srcType);
|
|
21
|
+
return {
|
|
22
|
+
email: user.email,
|
|
23
|
+
name: user.name,
|
|
24
|
+
...connection
|
|
25
|
+
};
|
|
18
26
|
}
|
|
19
27
|
|
|
20
28
|
export function isDocumentFieldOneOfFieldTypes<T extends FieldType>(documentField: ContentStoreTypes.DocumentField, fieldTypes: ReadonlyArray<T>): documentField is ContentStoreTypes.DocumentFieldForType<T> {
|
package/src/content-store.ts
CHANGED
|
@@ -48,6 +48,8 @@ import { mergeObjectWithDocument } from './utils/duplicate-document';
|
|
|
48
48
|
import { normalizeModels, validateModels } from './utils/model-utils';
|
|
49
49
|
import { IMAGE_MODEL } from './common/common-schema';
|
|
50
50
|
import { getDocumentObjectFromPreset, getPresetFromDocument } from './utils/preset-utils';
|
|
51
|
+
import { GitService } from './services';
|
|
52
|
+
import { CommandRunner } from '@stackbit/types';
|
|
51
53
|
|
|
52
54
|
export type HandleConfigAssets = <T extends Model>({ models, presets }: { models?: T[]; presets?: PresetMap }) => Promise<{ models: T[]; presets: PresetMap }>;
|
|
53
55
|
|
|
@@ -55,8 +57,11 @@ export interface ContentSourceOptions {
|
|
|
55
57
|
logger: CSITypes.Logger;
|
|
56
58
|
userLogger: CSITypes.Logger;
|
|
57
59
|
localDev: boolean;
|
|
60
|
+
staticAssetsPublicPath: string;
|
|
58
61
|
webhookUrl?: string;
|
|
59
|
-
|
|
62
|
+
runCommand: CommandRunner;
|
|
63
|
+
git: GitService;
|
|
64
|
+
userCommandSpawner?: UserCommandSpawner; //TODO remove
|
|
60
65
|
onSchemaChangeCallback: () => void;
|
|
61
66
|
onContentChangeCallback: (contentChanges: ContentStoreTypes.ContentChangeResult) => void;
|
|
62
67
|
handleConfigAssets: HandleConfigAssets;
|
|
@@ -73,7 +78,10 @@ export class ContentStore {
|
|
|
73
78
|
private readonly userLogger: CSITypes.Logger;
|
|
74
79
|
private readonly userCommandSpawner?: UserCommandSpawner;
|
|
75
80
|
private readonly localDev: boolean;
|
|
81
|
+
private readonly staticAssetsPublicPath: string;
|
|
76
82
|
private readonly webhookUrl?: string;
|
|
83
|
+
private readonly runCommand: CommandRunner;
|
|
84
|
+
private readonly git: GitService;
|
|
77
85
|
private readonly onSchemaChangeCallback: () => void;
|
|
78
86
|
private readonly onContentChangeCallback: (contentChanges: ContentStoreTypes.ContentChangeResult) => void;
|
|
79
87
|
private readonly handleConfigAssets: HandleConfigAssets;
|
|
@@ -93,7 +101,10 @@ export class ContentStore {
|
|
|
93
101
|
this.logger = options.logger.createLogger({ label: 'content-store' });
|
|
94
102
|
this.userLogger = options.userLogger.createLogger({ label: 'content-store' });
|
|
95
103
|
this.localDev = options.localDev;
|
|
104
|
+
this.staticAssetsPublicPath = options.staticAssetsPublicPath;
|
|
96
105
|
this.webhookUrl = options.webhookUrl;
|
|
106
|
+
this.runCommand = options.runCommand;
|
|
107
|
+
this.git = options.git;
|
|
97
108
|
this.userCommandSpawner = options.userCommandSpawner;
|
|
98
109
|
this.onSchemaChangeCallback = options.onSchemaChangeCallback;
|
|
99
110
|
this.onContentChangeCallback = options.onContentChangeCallback;
|
|
@@ -430,7 +441,9 @@ export class ContentStore {
|
|
|
430
441
|
userCommandSpawner: this.userCommandSpawner,
|
|
431
442
|
localDev: this.localDev,
|
|
432
443
|
webhookUrl: this.getWebhookUrl(contentSourceInstance.getContentSourceType(), contentSourceInstance.getProjectId()),
|
|
433
|
-
devAppRestartNeeded: this.devAppRestartNeeded
|
|
444
|
+
devAppRestartNeeded: this.devAppRestartNeeded,
|
|
445
|
+
runCommand: this.runCommand,
|
|
446
|
+
git: this.git
|
|
434
447
|
});
|
|
435
448
|
} else {
|
|
436
449
|
contentSourceInstance.stopWatchingContentUpdates();
|
|
@@ -1154,12 +1167,12 @@ export class ContentStore {
|
|
|
1154
1167
|
if (srcProjectId && srcType) {
|
|
1155
1168
|
const contentSourceId = getContentSourceId(srcType, srcProjectId);
|
|
1156
1169
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
1157
|
-
assets = mapStoreAssetsToAPIAssets(contentSourceData.assets, contentSourceData.defaultLocaleCode);
|
|
1170
|
+
assets = mapStoreAssetsToAPIAssets(contentSourceData.assets, this.staticAssetsPublicPath, contentSourceData.defaultLocaleCode);
|
|
1158
1171
|
} else {
|
|
1159
1172
|
assets = _.reduce(
|
|
1160
1173
|
this.contentSourceDataById,
|
|
1161
1174
|
(result: ContentStoreTypes.APIAsset[], contentSourceData) => {
|
|
1162
|
-
const assets = mapStoreAssetsToAPIAssets(contentSourceData.assets, contentSourceData.defaultLocaleCode);
|
|
1175
|
+
const assets = mapStoreAssetsToAPIAssets(contentSourceData.assets, this.staticAssetsPublicPath, contentSourceData.defaultLocaleCode);
|
|
1163
1176
|
return result.concat(assets);
|
|
1164
1177
|
},
|
|
1165
1178
|
[]
|
|
@@ -1733,7 +1746,7 @@ export class ContentStore {
|
|
|
1733
1746
|
contentSourceInstance: contentSourceData.instance,
|
|
1734
1747
|
defaultLocaleCode: contentSourceData.defaultLocaleCode
|
|
1735
1748
|
});
|
|
1736
|
-
return mapStoreAssetsToAPIAssets(storeAssets, locale);
|
|
1749
|
+
return mapStoreAssetsToAPIAssets(storeAssets, this.staticAssetsPublicPath, locale);
|
|
1737
1750
|
}
|
|
1738
1751
|
|
|
1739
1752
|
async deleteDocument({
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import _ from 'lodash';
|
|
2
|
+
import os from 'os';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { v4 as uuid } from 'uuid';
|
|
5
|
+
import fse from 'fs-extra';
|
|
6
|
+
|
|
7
|
+
import { GitServiceInterface, GitFileCommitDescriptor, GitAuthor, GitCommitLogEntry, Logger } from '@stackbit/types';
|
|
8
|
+
import { Worker } from '@stackbit/utils';
|
|
9
|
+
|
|
10
|
+
import { DocumentStatus } from '@stackbit/types';
|
|
11
|
+
import { CommandRunner } from '@stackbit/types';
|
|
12
|
+
|
|
13
|
+
const GIT_LOG_CHANGE_TYPES: Record<string, DocumentStatus> = {
|
|
14
|
+
M: 'modified',
|
|
15
|
+
A: 'added',
|
|
16
|
+
D: 'deleted'
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export class GitService implements GitServiceInterface {
|
|
20
|
+
private readonly repoUrl: string;
|
|
21
|
+
private readonly repoDir: string;
|
|
22
|
+
private readonly repoBranch: string;
|
|
23
|
+
private readonly repoPublishBranch: string;
|
|
24
|
+
private readonly worker: Worker;
|
|
25
|
+
private readonly runCommand: CommandRunner;
|
|
26
|
+
private readonly logger: Logger;
|
|
27
|
+
private readonly userLogger: Logger;
|
|
28
|
+
|
|
29
|
+
private branchFetched: boolean = false;
|
|
30
|
+
|
|
31
|
+
constructor(options: {
|
|
32
|
+
repoUrl: string;
|
|
33
|
+
repoDir: string;
|
|
34
|
+
repoBranch: string;
|
|
35
|
+
repoPublishBranch: string;
|
|
36
|
+
worker: Worker;
|
|
37
|
+
runCommand: CommandRunner;
|
|
38
|
+
logger: Logger;
|
|
39
|
+
userLogger: Logger;
|
|
40
|
+
}) {
|
|
41
|
+
this.repoUrl = options.repoUrl;
|
|
42
|
+
this.repoDir = options.repoDir;
|
|
43
|
+
this.repoBranch = options.repoBranch;
|
|
44
|
+
this.repoPublishBranch = options.repoPublishBranch;
|
|
45
|
+
this.worker = options.worker;
|
|
46
|
+
this.runCommand = options.runCommand;
|
|
47
|
+
this.logger = options.logger;
|
|
48
|
+
this.userLogger = options.userLogger;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
getRepoUrl() {
|
|
52
|
+
return this.repoUrl;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
getRepoBranch() {
|
|
56
|
+
return this.repoBranch;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
getRepoPublishBranch() {
|
|
60
|
+
return this.repoPublishBranch;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private async commit(author: GitAuthor, files: GitFileCommitDescriptor[]) {
|
|
64
|
+
const filePaths = _.map(files, 'filePath');
|
|
65
|
+
this.logger.debug('[git] Commit scheduled', filePaths);
|
|
66
|
+
return this.worker.schedule(async () => {
|
|
67
|
+
this.logger.debug('[git] Commit running', filePaths);
|
|
68
|
+
const message = files
|
|
69
|
+
.reduce((messages: string[], file) => {
|
|
70
|
+
messages.push(`${path.parse(file.filePath).base}: ${file.description}`);
|
|
71
|
+
return messages;
|
|
72
|
+
}, [])
|
|
73
|
+
.join('.\n');
|
|
74
|
+
await this.runCommand('git', ['add', ...filePaths], { cwd: this.repoDir });
|
|
75
|
+
await this.runCommand('git', ['commit', '--no-verify', '--author', `${author.name || author.email} <${author.email}>`, '-m', message], {
|
|
76
|
+
cwd: this.repoDir
|
|
77
|
+
});
|
|
78
|
+
this.logger.debug('[git] Commit done', filePaths);
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private push() {
|
|
83
|
+
this.logger.debug('[git] Push scheduled');
|
|
84
|
+
return this.worker.schedule(async () => {
|
|
85
|
+
this.logger.debug('[git] Push running');
|
|
86
|
+
await this.runCommand('rm', ['-rf', '.git/rebase-merge'], { cwd: this.repoDir }).catch((err) => {}); // fixes leftover rebase directory with autostash
|
|
87
|
+
await this.runCommand('git', ['pull', 'origin', this.repoBranch, '--rebase', '--autostash', '-Xtheirs'], { cwd: this.repoDir });
|
|
88
|
+
await this.runCommand('git', ['push', 'origin', this.repoBranch], { cwd: this.repoDir });
|
|
89
|
+
this.logger.debug('[git] Push done');
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async commitAndPush(author: GitAuthor, files: GitFileCommitDescriptor[]): Promise<void> {
|
|
94
|
+
await this.commit(author, files);
|
|
95
|
+
return this.push();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
pull(): Promise<void> {
|
|
99
|
+
this.logger.debug('[git] Pull scheduled');
|
|
100
|
+
return this.worker.schedule(async () => {
|
|
101
|
+
this.logger.debug('[git] Pull running');
|
|
102
|
+
await this.runCommand('git', ['pull', 'origin', '--rebase', '--autostash', '-Xtheirs'], { cwd: this.repoDir });
|
|
103
|
+
this.logger.debug('[git] Pull done');
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private async publishAll(author: GitAuthor) {
|
|
108
|
+
this.logger.debug('[git] Publish all started');
|
|
109
|
+
const publishDir = path.join(os.tmpdir(), uuid());
|
|
110
|
+
await this.runCommand('git', ['clone', this.repoUrl, '--branch', this.repoPublishBranch, publishDir]);
|
|
111
|
+
try {
|
|
112
|
+
await this.runCommand('git', ['merge', `origin/${this.repoBranch}`, this.repoPublishBranch, '-Xtheirs'], { cwd: publishDir });
|
|
113
|
+
await this.runCommand('git', ['commit', '--author', `${author.name || author.email} <${author.email}>`, `-m`, 'Publish'], {
|
|
114
|
+
cwd: publishDir
|
|
115
|
+
}).catch(() => {});
|
|
116
|
+
await this.runCommand('git', ['push', 'origin'], { cwd: publishDir });
|
|
117
|
+
} finally {
|
|
118
|
+
await fse.remove(publishDir);
|
|
119
|
+
}
|
|
120
|
+
this.logger.debug('[git] Publish all done');
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
private async publishFiles(author: GitAuthor, filePaths: string[]) {
|
|
124
|
+
this.logger.debug('[git] Publish files started', filePaths);
|
|
125
|
+
const publishDir = path.join(os.tmpdir(), uuid());
|
|
126
|
+
await this.runCommand('git', ['clone', this.repoUrl, '--branch', this.repoPublishBranch, publishDir]);
|
|
127
|
+
try {
|
|
128
|
+
await Promise.all(
|
|
129
|
+
filePaths.map(async (filePath) => {
|
|
130
|
+
const destFilePath = path.join(publishDir, filePath);
|
|
131
|
+
await fse.ensureDir(path.dirname(destFilePath));
|
|
132
|
+
return fse.copy(path.join(this.repoDir, filePath), destFilePath);
|
|
133
|
+
})
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
await this.runCommand('git', ['checkout', '-b', 'stackbit-publish-branch'], { cwd: publishDir });
|
|
137
|
+
await this.runCommand('git', ['add', ...filePaths], { cwd: publishDir });
|
|
138
|
+
await this.runCommand('git', ['commit', '--author', `${author.name || author.email} <${author.email}>`, `-m`, 'Publish'], {
|
|
139
|
+
cwd: publishDir
|
|
140
|
+
}).catch((err) => {
|
|
141
|
+
return;
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
await this.runCommand('git', ['checkout', this.repoBranch], { cwd: publishDir });
|
|
145
|
+
await this.runCommand('git', ['merge', 'stackbit-publish-branch', this.repoBranch, '-Xtheirs'], { cwd: publishDir });
|
|
146
|
+
|
|
147
|
+
await this.runCommand('git', ['checkout', this.repoPublishBranch], { cwd: publishDir });
|
|
148
|
+
await this.runCommand('git', ['merge', 'stackbit-publish-branch', this.repoPublishBranch, '-Xtheirs'], { cwd: publishDir });
|
|
149
|
+
|
|
150
|
+
await this.runCommand('git', ['push', 'origin', this.repoPublishBranch, this.repoBranch], { cwd: publishDir });
|
|
151
|
+
} finally {
|
|
152
|
+
await fse.remove(publishDir);
|
|
153
|
+
}
|
|
154
|
+
this.logger.debug('[git] Publish files done', filePaths);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
publish(author: GitAuthor, filePaths?: string[]): Promise<void> {
|
|
158
|
+
this.logger.debug('[git] Publish scheduled');
|
|
159
|
+
return this.worker.schedule(async () => {
|
|
160
|
+
if (filePaths) {
|
|
161
|
+
if (!filePaths.length) {
|
|
162
|
+
this.logger.debug('[git] Nothing to publish');
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
return this.publishFiles(author, filePaths);
|
|
166
|
+
} else {
|
|
167
|
+
return this.publishAll(author);
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
private parseGitCommitAuthor(author?: string) {
|
|
173
|
+
if (!author) {
|
|
174
|
+
return author;
|
|
175
|
+
}
|
|
176
|
+
const regex = /(.*)\((.*)\)/;
|
|
177
|
+
const match = author.match(regex);
|
|
178
|
+
if (match) {
|
|
179
|
+
const [authorEmail, authorName] = match.slice(1);
|
|
180
|
+
|
|
181
|
+
if (authorName === 'Stackbit Code Editor') {
|
|
182
|
+
return 'stackbit';
|
|
183
|
+
}
|
|
184
|
+
return authorEmail ? authorEmail.toLowerCase() : author;
|
|
185
|
+
}
|
|
186
|
+
return author;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
async diff(): Promise<string[]> {
|
|
190
|
+
this.logger.debug('[git] Diff check scheduled');
|
|
191
|
+
return this.worker.schedule(async () => {
|
|
192
|
+
this.logger.debug('[git] Diff check running');
|
|
193
|
+
const result = await this.runCommand(
|
|
194
|
+
'git',
|
|
195
|
+
[
|
|
196
|
+
'diff',
|
|
197
|
+
'--name-only',
|
|
198
|
+
'--no-renames', // this flag makes sure we get both old and new name of renamed file
|
|
199
|
+
`origin/${this.repoPublishBranch}..${this.repoBranch}`
|
|
200
|
+
],
|
|
201
|
+
{ cwd: this.repoDir }
|
|
202
|
+
);
|
|
203
|
+
this.logger.debug('[git] Diff check done');
|
|
204
|
+
return result.stdout.split('\n').filter(Boolean);
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async commitLog(): Promise<GitCommitLogEntry[]> {
|
|
209
|
+
this.logger.debug('[git] Changes check scheduled');
|
|
210
|
+
return this.worker.schedule(async () => {
|
|
211
|
+
this.logger.debug('[git] Changes check running');
|
|
212
|
+
if (!this.branchFetched) {
|
|
213
|
+
await this.runCommand('git', ['fetch', 'origin', `${this.repoPublishBranch}:${this.repoPublishBranch}`], { cwd: this.repoDir });
|
|
214
|
+
this.branchFetched = true;
|
|
215
|
+
}
|
|
216
|
+
const logResult = await this.runCommand(
|
|
217
|
+
'git',
|
|
218
|
+
['log', '--pretty=format:commit:%H%n%at%n%ae%x28%an%x29', '--name-status', `${this.repoPublishBranch}..${this.repoBranch}`],
|
|
219
|
+
{ cwd: this.repoDir }
|
|
220
|
+
);
|
|
221
|
+
this.logger.debug('[git] Changes check done');
|
|
222
|
+
return logResult.stdout
|
|
223
|
+
.split('commit:')
|
|
224
|
+
.filter(Boolean)
|
|
225
|
+
.map((rawCommit) => {
|
|
226
|
+
const split = rawCommit.trim().split('\n');
|
|
227
|
+
return {
|
|
228
|
+
author: this.parseGitCommitAuthor(split[2]),
|
|
229
|
+
timestamp: split[1] ? new Date(parseInt(split[1]) * 1000) : undefined,
|
|
230
|
+
commitHash: split[0],
|
|
231
|
+
changes: split
|
|
232
|
+
.slice(3)
|
|
233
|
+
.map((line) => line.trim().split(/\t/))
|
|
234
|
+
.filter(Boolean)
|
|
235
|
+
.filter(([status, filename]) => status && filename)
|
|
236
|
+
.map(([status, filename]) => ({
|
|
237
|
+
status: GIT_LOG_CHANGE_TYPES[status!] || 'modified',
|
|
238
|
+
filePath: filename
|
|
239
|
+
}))
|
|
240
|
+
};
|
|
241
|
+
})
|
|
242
|
+
.reverse();
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import childProcess, { ChildProcessWithoutNullStreams } from 'child_process';
|
|
2
|
+
import { CommandRunner, RunResult } from '@stackbit/types';
|
|
3
|
+
|
|
4
|
+
export function getCommandRunner(commandRunnerOptions: { env: NodeJS.ProcessEnv; uid?: number }): CommandRunner {
|
|
5
|
+
return (command: string, args?: string[], options?: { cwd?: string; shell?: boolean; env?: NodeJS.ProcessEnv }): Promise<RunResult> => {
|
|
6
|
+
return getProcessPromise(
|
|
7
|
+
childProcess.spawn(command, args, {
|
|
8
|
+
cwd: options?.cwd,
|
|
9
|
+
shell: options?.shell,
|
|
10
|
+
env: {
|
|
11
|
+
...commandRunnerOptions.env,
|
|
12
|
+
...options?.env
|
|
13
|
+
},
|
|
14
|
+
...(commandRunnerOptions.uid ? { uid: commandRunnerOptions.uid } : {})
|
|
15
|
+
})
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function getProcessPromise(p: ChildProcessWithoutNullStreams): Promise<{
|
|
21
|
+
stdout: string;
|
|
22
|
+
stderr: string;
|
|
23
|
+
exitCode?: number;
|
|
24
|
+
err?: Error;
|
|
25
|
+
}> {
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
let stdout = '';
|
|
28
|
+
let stderr = '';
|
|
29
|
+
p.stdout.on('data', (out) => (stdout += out));
|
|
30
|
+
p.stderr.on('data', (out) => (stderr += out));
|
|
31
|
+
p.on('exit', (exitCode) => {
|
|
32
|
+
if (exitCode !== 0) {
|
|
33
|
+
reject({
|
|
34
|
+
err: new Error(`process exited with code: ${exitCode}, stderr: ${stderr}`),
|
|
35
|
+
stdout,
|
|
36
|
+
stderr,
|
|
37
|
+
exitCode
|
|
38
|
+
});
|
|
39
|
+
} else {
|
|
40
|
+
resolve({
|
|
41
|
+
stdout,
|
|
42
|
+
stderr
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
p.on('error', (err) => {
|
|
47
|
+
reject({
|
|
48
|
+
err,
|
|
49
|
+
stdout,
|
|
50
|
+
stderr
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
}
|