diff --git a/packages/netlify-cms-lib-util/src/API.ts b/packages/netlify-cms-lib-util/src/API.ts index 4c4e83be..cac92155 100644 --- a/packages/netlify-cms-lib-util/src/API.ts +++ b/packages/netlify-cms-lib-util/src/API.ts @@ -1,31 +1,6 @@ import { asyncLock, AsyncLock } from './asyncLock'; import unsentRequest from './unsentRequest'; -export const CMS_BRANCH_PREFIX = 'cms'; -export const DEFAULT_PR_BODY = 'Automatically generated by Netlify CMS'; -export const MERGE_COMMIT_MESSAGE = 'Automatically generated. Merged on Netlify CMS.'; - -const NETLIFY_CMS_LABEL_PREFIX = 'netlify-cms/'; -export const isCMSLabel = (label: string) => label.startsWith(NETLIFY_CMS_LABEL_PREFIX); -export const labelToStatus = (label: string) => label.substr(NETLIFY_CMS_LABEL_PREFIX.length); -export const statusToLabel = (status: string) => `${NETLIFY_CMS_LABEL_PREFIX}${status}`; - -export const generateContentKey = (collectionName: string, slug: string) => - `${collectionName}/${slug}`; - -export const parseContentKey = (contentKey: string) => { - const index = contentKey.indexOf('/'); - return { collection: contentKey.substr(0, index), slug: contentKey.substr(index + 1) }; -}; - -export const contentKeyFromBranch = (branch: string) => { - return branch.substring(`${CMS_BRANCH_PREFIX}/`.length); -}; - -export const branchFromContentKey = (contentKey: string) => { - return `${CMS_BRANCH_PREFIX}/${contentKey}`; -}; - export interface FetchError extends Error { status: number; } diff --git a/packages/netlify-cms-lib-util/src/APIUtils.ts b/packages/netlify-cms-lib-util/src/APIUtils.ts new file mode 100644 index 00000000..d959ea91 --- /dev/null +++ b/packages/netlify-cms-lib-util/src/APIUtils.ts @@ -0,0 +1,24 @@ +export const CMS_BRANCH_PREFIX = 'cms'; +export const DEFAULT_PR_BODY = 'Automatically generated by Netlify CMS'; +export const MERGE_COMMIT_MESSAGE = 'Automatically generated. Merged on Netlify CMS.'; + +const NETLIFY_CMS_LABEL_PREFIX = 'netlify-cms/'; +export const isCMSLabel = (label: string) => label.startsWith(NETLIFY_CMS_LABEL_PREFIX); +export const labelToStatus = (label: string) => label.substr(NETLIFY_CMS_LABEL_PREFIX.length); +export const statusToLabel = (status: string) => `${NETLIFY_CMS_LABEL_PREFIX}${status}`; + +export const generateContentKey = (collectionName: string, slug: string) => + `${collectionName}/${slug}`; + +export const parseContentKey = (contentKey: string) => { + const index = contentKey.indexOf('/'); + return { collection: contentKey.substr(0, index), slug: contentKey.substr(index + 1) }; +}; + +export const contentKeyFromBranch = (branch: string) => { + return branch.substring(`${CMS_BRANCH_PREFIX}/`.length); +}; + +export const branchFromContentKey = (contentKey: string) => { + return `${CMS_BRANCH_PREFIX}/${contentKey}`; +}; diff --git a/packages/netlify-cms-lib-util/src/__tests__/api.spec.js b/packages/netlify-cms-lib-util/src/__tests__/api.spec.js index a4a86213..675ff045 100644 --- a/packages/netlify-cms-lib-util/src/__tests__/api.spec.js +++ b/packages/netlify-cms-lib-util/src/__tests__/api.spec.js @@ -1,58 +1,5 @@ import * as api from '../API'; describe('Api', () => { - describe('generateContentKey', () => { - it('should generate content key', () => { - expect(api.generateContentKey('posts', 'dir1/dir2/post-title')).toBe( - 'posts/dir1/dir2/post-title', - ); - }); - }); - - describe('parseContentKey', () => { - it('should parse content key', () => { - expect(api.parseContentKey('posts/dir1/dir2/post-title')).toEqual({ - collection: 'posts', - slug: 'dir1/dir2/post-title', - }); - }); - }); - - describe('isCMSLabel', () => { - it('should return true for CMS label', () => { - expect(api.isCMSLabel('netlify-cms/draft')).toBe(true); - }); - - it('should return false for non CMS label', () => { - expect(api.isCMSLabel('other/label')).toBe(false); - }); - }); - - describe('labelToStatus', () => { - it('should get status from label', () => { - expect(api.labelToStatus('netlify-cms/draft')).toBe('draft'); - }); - }); - - describe('statusToLabel', () => { - it('should generate label from status', () => { - expect(api.statusToLabel('draft')).toBe('netlify-cms/draft'); - }); - }); - - describe('isPreviewContext', () => { - it('should return true for default preview context', () => { - expect(api.isPreviewContext('deploy', '')).toBe(true); - }); - - it('should return false for non default preview context', () => { - expect(api.isPreviewContext('other', '')).toBe(false); - }); - - it('should return true for custom preview context', () => { - expect(api.isPreviewContext('ci/custom_preview', 'ci/custom_preview')).toBe(true); - }); - }); - describe('getPreviewStatus', () => { it('should return preview status on matching context', () => { expect(api.getPreviewStatus([{ context: 'deploy' }])).toEqual({ context: 'deploy' }); diff --git a/packages/netlify-cms-lib-util/src/__tests__/apiUtils.spec.js b/packages/netlify-cms-lib-util/src/__tests__/apiUtils.spec.js new file mode 100644 index 00000000..b565c916 --- /dev/null +++ b/packages/netlify-cms-lib-util/src/__tests__/apiUtils.spec.js @@ -0,0 +1,41 @@ +import * as apiUtils from '../APIUtils'; +describe('APIUtils', () => { + describe('generateContentKey', () => { + it('should generate content key', () => { + expect(apiUtils.generateContentKey('posts', 'dir1/dir2/post-title')).toBe( + 'posts/dir1/dir2/post-title', + ); + }); + }); + + describe('parseContentKey', () => { + it('should parse content key', () => { + expect(apiUtils.parseContentKey('posts/dir1/dir2/post-title')).toEqual({ + collection: 'posts', + slug: 'dir1/dir2/post-title', + }); + }); + }); + + describe('isCMSLabel', () => { + it('should return true for CMS label', () => { + expect(apiUtils.isCMSLabel('netlify-cms/draft')).toBe(true); + }); + + it('should return false for non CMS label', () => { + expect(apiUtils.isCMSLabel('other/label')).toBe(false); + }); + }); + + describe('labelToStatus', () => { + it('should get status from label', () => { + expect(apiUtils.labelToStatus('netlify-cms/draft')).toBe('draft'); + }); + }); + + describe('statusToLabel', () => { + it('should generate label from status', () => { + expect(apiUtils.statusToLabel('draft')).toBe('netlify-cms/draft'); + }); + }); +}); diff --git a/packages/netlify-cms-lib-util/src/index.ts b/packages/netlify-cms-lib-util/src/index.ts index a14b88f4..62ae191d 100644 --- a/packages/netlify-cms-lib-util/src/index.ts +++ b/packages/netlify-cms-lib-util/src/index.ts @@ -42,6 +42,14 @@ import { import { readFile, readFileMetadata, + isPreviewContext, + getPreviewStatus, + PreviewState, + FetchError as FE, + ApiRequest as AR, + requestWithBackoff, +} from './API'; +import { CMS_BRANCH_PREFIX, generateContentKey, isCMSLabel, @@ -49,16 +57,10 @@ import { statusToLabel, DEFAULT_PR_BODY, MERGE_COMMIT_MESSAGE, - isPreviewContext, - getPreviewStatus, - PreviewState, - FetchError as FE, parseContentKey, branchFromContentKey, contentKeyFromBranch, - ApiRequest as AR, - requestWithBackoff, -} from './API'; +} from './APIUtils'; import { createPointerFile, getLargeMediaFilteredMediaFiles, diff --git a/packages/netlify-cms-proxy-server/src/middlewares/localGit/index.ts b/packages/netlify-cms-proxy-server/src/middlewares/localGit/index.ts index ee73c155..9752650e 100644 --- a/packages/netlify-cms-proxy-server/src/middlewares/localGit/index.ts +++ b/packages/netlify-cms-proxy-server/src/middlewares/localGit/index.ts @@ -9,7 +9,7 @@ import { CMS_BRANCH_PREFIX, statusToLabel, labelToStatus, -} from 'netlify-cms-lib-util/src/API'; +} from 'netlify-cms-lib-util/src/APIUtils'; import { defaultSchema, joi } from '../joi'; import { diff --git a/packages/netlify-cms-proxy-server/src/semaphore.d.ts b/packages/netlify-cms-proxy-server/src/semaphore.d.ts deleted file mode 100644 index 8c09e2a0..00000000 --- a/packages/netlify-cms-proxy-server/src/semaphore.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -declare module 'semaphore' { - export type Semaphore = { take: (f: Function) => void; leave: () => void }; - const semaphore: (count: number) => Semaphore; - export default semaphore; -}