Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
fix(build command): remove unnecessary spaces on built documents
Browse files Browse the repository at this point in the history
  • Loading branch information
fox1t committed Jul 1, 2020
1 parent 9f42e80 commit 5eafb39
Show file tree
Hide file tree
Showing 9 changed files with 1,061 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
ignorePatterns: ['bin', 'commitlint.config.js'],
ignorePatterns: ['bin', 'commitlint.config.js', 'node_modules/', 'test-typings', '.eslintrc.js'],
extends: [
'plugin:@typescript-eslint/recommended',
'prettier',
Expand Down
2 changes: 1 addition & 1 deletion bin/ddoc/build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async function build(src, opts) {
return val.toString();
}
return val;
}, 1);
}, 1).replace(/ +/g, ' ');
await writeFile(path_1.default.join(dest, `${filename}.json`), stringifiedDesign);
}
catch (error) {
Expand Down
832 changes: 832 additions & 0 deletions main-designs.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"mkdirp": "~1.0.3",
"require-from-string": "~2.0.2",
"sade": "~1.7.3",
"typescript": "~3.9.3"
"typescript": "~3.9.5"
},
"devDependencies": {
"@commitlint/cli": "~9.0.1",
Expand Down
2 changes: 1 addition & 1 deletion src/ddoc/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export default async function build(src: string, opts: { config: string }) {
return val
},
1,
)
).replace(/ +/g, ' ')
await writeFile(path.join(dest, `${filename}.json`), stringifiedDesign)
} catch (error) {
errors.push({ file: srcPath, error })
Expand Down
4 changes: 2 additions & 2 deletions src/ddoc/extract/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ export default async function (
for (const view of Object.keys(doc.views)) {
if (doc.views[view].map) {
const name = `${doc._id}-${doc._rev}.views.${view}.map`
methods.set(name, doc.views[view].map!)
methods.set(name, doc.views[view].map!) // eslint-disable-line
doc.views[view].map = name
}
if (doc.views[view].reduce) {
const name = `${doc._id}-${doc._rev}.views.${view}.reduce`
methods.set(name, doc.views[view].reduce!)
methods.set(name, doc.views[view].reduce!) // eslint-disable-line
doc.views[view].reduce = name
}
}
Expand Down
197 changes: 197 additions & 0 deletions test-typings/design-functions-context.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
// Reference: https://docs.couchdb.org/en/latest/query-server/javascript.html#design-functions-context

declare namespace CouchDB {
type HttpMethods = 'HEAD' | 'GET' | 'POST' | 'PUT' | 'DELETE' | 'OPTIONS' | 'TRACE'
interface InitResp {
code?: number
json?: object
body?: string
base64?: string
headers?: any
stop?: boolean
}

interface UserCtx {
db: string
name: string | null
roles: string[]
}

interface SecurityObject {
admins: {
names: string[]
roles: string[]
}
members: {
names: string[]
roles: string[]
}
}

interface DataBaseInformationObject {
db_name: string
committed_update_seq: number
doc_count: number
doc_del_count: number
compact_running: boolean
disk_format_version: number
disk_size: number
instance_start_time: string
purge_seq: number
update_seq: number
sizes: {
active: number
disk: number
external: number
}
}

interface RequestObject {
body: string
cookie: Record<string, any>
form: Record<string, any>
headers: Record<string, any>
id: string | null
info: CouchDB.DataBaseInformationObject
method: HttpMethods
path: string[]
peer: string
query: Record<string, any>
raw_path: string
requested_path: string[]
secObj: CouchDB.SecurityObject
userCtx: CouchDB.UserCtx
uuid: string
}

interface RewriteRule {
method?: string
from: string
to: string
query: Record<string, any>
}

interface Request2Object {
body: string
cookie: Record<string, any>
headers: Record<string, any>
method: HttpMethods
path: string[]
peer: string
query: Record<string, any>
requested_path: string[]
raw_path: string
secObj: CouchDB.SecurityObject
userCtx: CouchDB.UserCtx
}

interface RewriteReturnObject {
path: string
code?: number
query?: Record<string, any>
headers?: any
method?: HttpMethods
body?: string
}

interface ResponseObject {
code?: number
json?: any
body?: string
base64?: string
headers?: any
stop?: boolean
}

interface ViewHeadInformation {
total_rows: number
offset: number
}
}
// reference: https://docs.couchdb.org/en/stable/ddocs/views/collation.html
type kyesBaseTypes = null | boolean | number | string | object
type emitKeys = kyesBaseTypes | kyesBaseTypes[]
/**
* Emits a key-value pair for further processing by CouchDB after the map function is done.
* @param {string} key - The view key
* @param {any} value - The key’s associated value
* @returns {void}
*/
declare function emit<T>(key: emitKeys, value?: T): void
/**
* @deprecated since version 2.0
* Extracts the next row from a related view result.
* @returns {Object} - View result row
*/
declare function getRow<T>(): T

/**
* A helper function to check if the provided value is an Array.
* @param {Object} obj - Any JavaScript value
* @returns {boolean}
*/
declare function isArray<T>(obj: T): boolean

/**
* Log a message to the CouchDB log (at the INFO level).
* @param {string} message - Message to be logged
* @returns {void}
*/
declare function log(message: string): void

/**
* @deprecated since version 2.0
* Registers callable handler for specified MIME key.
* @param {string} key - MIME key previously defined by registerType()
* @param {Function} value - MIME type handler
* @returns {void}
*/
declare function provides(key: string, func: Function): string | CouchDB.ResponseObject

/**
* @deprecated since version 2.0
* Registers list of MIME types by associated key.
* @param {string} key - MIME types
* @param {string[]} mimes - MMIME types enumeration
* @returns {void}
*/
declare function registerType(key: string, mimes: string[]): void

/**
* @deprecated since version 2.0
* Sends a single string chunk in response.
* @param {string} chunk - Text chunk
* @returns {void}
*/
declare function send(chunk: string): void

/**
* @deprecated since version 2.0
* Initiates chunked response. As an option, a custom response object may be sent at this point. For list-functions only!
* @param {Object} init_resp - InitResp object
* @returns {void}
*/
// eslint-disable-next-line
declare function start(init_resp?: CouchDB.InitResp): void

/**
* Sum arr’s items.
* @param {number[]} arr - Array of numbers
* @returns {number}
*/
declare function sum(arr: number[]): number

/**
* Encodes obj to JSON string. This is an alias for the JSON.stringify method.
* @param {any} obj - Array of numbers
* @returns {string}
*/
declare function toJSON(obj: any): string

/**
* Reduce functions take two required arguments of keys and values lists - the result of the related map function - and an optional third value which indicates if rereduce mode is active or not.
* Rereduce is used for additional reduce values list, so when it is true there is no information about related keys (first argument is null).
* @param {string[] | null} keys - Array of pairs of docid-key for related map function results. Always null if rereduce is running (has true value).
* @returns {void}
*/
declare type redfun = (keys: [string, string][] | null, values: any[], rereduce?: boolean) => any
12 changes: 11 additions & 1 deletion test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import mkdirp from 'mkdirp'
import rimraf from 'rimraf'

import extract from '../src/ddoc/extract/extract'
import build from '../src/ddoc/build/build'

const dummyData = join(__dirname, './dummy-data')

Expand Down Expand Up @@ -35,6 +36,15 @@ test('Should extract design documents from a backup', async (t: any) => {
})
})

t.tearDown(() => rimraf.sync(destinationFolder))
t.test('from TypeScript', async () => {
await build(destinationFolder, {
config: join(__dirname, './tsconfig.build-test.json'),
})
})

t.tearDown(() => {
rimraf.sync(destinationFolder)
rimraf.sync(join(dummyData, 'designs'))
})
t.end()
})
15 changes: 15 additions & 0 deletions test/tsconfig.build-test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"include": [
"./dummy-data/output/main-designs"
],
"exclude": [
"node_modules"
],
"compilerOptions": {
"typeRoots": [
"../test-typings/design-functions-context.d.ts"
],
"outDir": "./dummy-data/designs",
"sourceMap": false
}
}

0 comments on commit 5eafb39

Please sign in to comment.