sourcebit-source-filesystem
Advanced tools
Comparing version 0.1.3 to 0.1.4
{ | ||
"name": "sourcebit-source-filesystem", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Sourcebit filesystem source plugin", | ||
@@ -28,8 +28,8 @@ "main": "src/index.js", | ||
"dependencies": { | ||
"@stackbit/schema": "^0.1.1", | ||
"@stackbit/sdk": "^0.2.16", | ||
"@stackbit/utils": "^0.1.0", | ||
"chokidar": "^3.4.0", | ||
"fs-extra": "^9.0.1", | ||
"lodash": "^4.17.15" | ||
"chokidar": "^3.5.2", | ||
"fs-extra": "^10.0.0", | ||
"lodash": "^4.17.21" | ||
} | ||
} |
@@ -5,3 +5,3 @@ const chokidar = require('chokidar'); | ||
const _ = require('lodash'); | ||
const { loadModels } = require('@stackbit/schema'); | ||
const { loadConfig } = require('@stackbit/sdk'); | ||
const { parseFile, mapPromise, readDirRecursively } = require('@stackbit/utils'); | ||
@@ -36,9 +36,9 @@ const { matchObjectsToModels } = require('./models-matcher'); | ||
log('loading stackbit.yaml and models...'); | ||
const { models, stackbitYaml } = await loadStackbitYaml(stackbitYamlPath) | ||
setPluginContext({ models, stackbitYaml }); | ||
const config = await loadStackbitYaml(); | ||
setPluginContext({ config }); | ||
// if 'sources' were not specified, use 'pagesDir' and 'dataDir' from stackbit.yaml | ||
if (_.isEmpty(sources)) { | ||
const pagesDir = _.get(stackbitYaml, 'pagesDir', ''); | ||
const dataDir = _.get(stackbitYaml, 'dataDir', ''); | ||
const pagesDir = _.get(config, 'pagesDir', ''); | ||
const dataDir = _.get(config, 'dataDir', ''); | ||
sources = [ | ||
@@ -70,4 +70,4 @@ { name: 'pages', path: pagesDir }, | ||
log('reloading stackbit.yaml and models...'); | ||
const { models, stackbitYaml } = await loadStackbitYaml(stackbitYamlPath) | ||
setPluginContext({ models, stackbitYaml }); | ||
const config = await loadStackbitYaml(); | ||
setPluginContext({ config }); | ||
} | ||
@@ -105,8 +105,8 @@ | ||
let objects = context.files; | ||
if (context.stackbitYaml && context.models) { | ||
objects = matchObjectsToModels(context.files, context.models, { | ||
if (context.config) { | ||
objects = matchObjectsToModels(context.files, context.config.models, { | ||
pageObjectsPredicate: _.matches({__metadata: {sourceName: 'pages'}}), | ||
dataObjectsPredicate: _.matches({__metadata: {sourceName: 'data'}}), | ||
pageLayoutKey: ['frontmatter', _.get(context, 'stackbitYaml.pageLayoutKey', 'layout')], | ||
dataTypeKey: _.get(context, 'stackbitYaml.objectTypeKey', 'type'), | ||
pageLayoutKey: ['frontmatter', _.get(context.config, 'pageLayoutKey', 'layout')], | ||
dataTypeKey: _.get(context.config, 'objectTypeKey', 'type'), | ||
objectIdKeyPath: '__metadata.id', | ||
@@ -124,8 +124,11 @@ objectFileKeyPath: '__metadata.relSourcePath', | ||
async function loadStackbitYaml(stackbitYamlPath) { | ||
log('loading stackbit.yaml...'); | ||
const stackbitYaml = await parseFile(stackbitYamlPath); | ||
const models = loadModels(stackbitYaml.models); | ||
log(`loaded stackbit.yaml, found ${models.length} models`); | ||
return { models, stackbitYaml }; | ||
async function loadStackbitYaml() { | ||
const cwd = process.cwd(); | ||
const { config } = await loadConfig({ dirPath: cwd }); | ||
if (!config) { | ||
log('failed to load stackbit.yaml'); | ||
return null; | ||
} | ||
log(`loaded stackbit.yaml, found ${config.models.length} models`); | ||
return config; | ||
} | ||
@@ -132,0 +135,0 @@ |
const path = require('path'); | ||
const _ = require('lodash'); | ||
const { getModelByQuery } = require('@stackbit/schema'); | ||
const { getModelByQuery } = require('@stackbit/sdk'); | ||
@@ -10,2 +10,10 @@ | ||
function log(message) { | ||
console.log(`[sourcebit-source-filesystem] ${message}`); | ||
} | ||
function logError(message) { | ||
console.error(`[sourcebit-source-filesystem] ${message}`); | ||
} | ||
function matchObjectsToModels(objects, models, { pageObjectsPredicate, dataObjectsPredicate, pageLayoutKey, dataTypeKey, objectIdKeyPath, objectFileKeyPath, source, mergeDataModels, logMatchedModels }) { | ||
@@ -16,34 +24,28 @@ const modelsByName = _.keyBy(models, 'name'); | ||
console.log('matching files to models...'); | ||
log('matching files to models...'); | ||
const modelsByObjectIds = _.reduce(objects, (accum, object) => { | ||
if (pageObjectsPredicate(object)) { | ||
// return matchPageModel(object, pageModels, modelsByName); | ||
let model; | ||
try { | ||
model = getModelByQuery({ | ||
filePath: _.get(object, objectFileKeyPath), | ||
type: _.get(object, pageLayoutKey), | ||
modelTypeKeyPath: 'layout' | ||
}, pageModels); | ||
} catch (err) { | ||
console.error(err.message); | ||
return accum; | ||
const { model, error } = getModelByQuery({ | ||
filePath: _.get(object, objectFileKeyPath), | ||
type: _.get(object, pageLayoutKey), | ||
modelTypeKeyPath: 'layout' | ||
}, pageModels); | ||
if (error) { | ||
logError(error.message); | ||
} else { | ||
const objectId = _.get(object, objectIdKeyPath); | ||
accum[objectId] = model; | ||
} | ||
const objectId = _.get(object, objectIdKeyPath); | ||
accum[objectId] = model; | ||
} else if (dataObjectsPredicate(object)) { | ||
// return matchDataModel(object, dataModelsByFilePath, modelsByName); | ||
let model; | ||
try { | ||
model = getModelByQuery({ | ||
filePath: _.get(object, objectFileKeyPath), | ||
type: _.get(object, dataTypeKey), | ||
modelTypeKeyPath: 'name' | ||
}, dataModels); | ||
} catch (err) { | ||
console.error(err.message); | ||
return accum; | ||
const { model, error } = getModelByQuery({ | ||
filePath: _.get(object, objectFileKeyPath), | ||
type: _.get(object, dataTypeKey), | ||
modelTypeKeyPath: 'name' | ||
}, dataModels); | ||
if (error) { | ||
logError(error.message); | ||
} else { | ||
const objectId = _.get(object, objectIdKeyPath); | ||
accum[objectId] = model; | ||
} | ||
const objectId = _.get(object, objectIdKeyPath); | ||
accum[objectId] = model; | ||
} | ||
@@ -53,7 +55,7 @@ return accum; | ||
console.log(`${_.size(modelsByObjectIds)} of ${_.size(objects)} files were matched to models`); | ||
log(`${_.size(modelsByObjectIds)} of ${_.size(objects)} files were matched to models`); | ||
if (logMatchedModels) { | ||
const modelNames = _.uniq(_.map(modelsByObjectIds, 'name')); | ||
const longestModelName = _.maxBy(modelNames, _.size); | ||
console.log('matched models:' + _.map(modelsByObjectIds, (model, objectId) => '\n ' + _.padEnd(model.name, longestModelName.length) + ' : ' + objectId).join('')); | ||
log('matched models:' + _.map(modelsByObjectIds, (model, objectId) => '\n ' + _.padEnd(model.name, longestModelName.length) + ' : ' + objectId).join('')); | ||
} | ||
@@ -86,3 +88,3 @@ | ||
if (!_.isPlainObject(data)) { | ||
console.error(`value must be an object, ${location}`); | ||
logError(`value must be an object, ${location}`); | ||
return data; | ||
@@ -125,3 +127,3 @@ } | ||
if (!_.has(fieldModel, 'models') || !_.isArray(fieldModel.models) || fieldModel.models.length < 1) { | ||
console.error(`field of type 'model' must have 'models' property having an array with at least one model name, ${location}`); | ||
logError(`field of type 'model' must have 'models' property with array having at least one model name, ${location}`); | ||
return fieldValue; | ||
@@ -133,3 +135,3 @@ } | ||
if (!_.has(modelsByName, modelName)) { | ||
console.error(`the values of the 'models' array of the field of type 'model' must be the names of existing models, ${location}`); | ||
logError(`the 'models' array of the field of type 'model' must include the names of existing models, ${location}`); | ||
return fieldValue; | ||
@@ -139,3 +141,3 @@ } | ||
if (!_.has(fieldValue, 'type')) { | ||
console.error(`object referenced by a field of type 'model' having more than one model in 'models' array, must have 'type' property specifying the name of the object's model, ${location}`); | ||
logError(`object referenced by a field of type 'model' having more than one model in 'models' array, must have 'type' property specifying the name of the object's model, ${location}`); | ||
return fieldValue; | ||
@@ -145,3 +147,3 @@ } | ||
if (!_.has(modelsByName, modelName)) { | ||
console.error(`the value of the 'type' property of the object referenced by a field of type 'model' must be the name of an existing model, ${location}`); | ||
logError(`the value of the 'type' property of the object referenced by a field of type 'model' must be the name of an existing model, ${location}`); | ||
return fieldValue; | ||
@@ -160,3 +162,3 @@ } | ||
if (!_.isArray(fieldValue)) { | ||
console.error(`the value referenced by a field of type 'list' must be an array, ${location}`); | ||
logError(`the value referenced by a field of type 'list' must be an array, ${location}`); | ||
return fieldValue; | ||
@@ -163,0 +165,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
318
16268
+ Added@stackbit/sdk@^0.2.16
+ Added@esbuild/linux-loong64@0.14.54(transitive)
+ Added@hapi/hoek@9.3.0(transitive)
+ Added@hapi/topo@5.1.0(transitive)
+ Added@octokit/auth-token@2.5.0(transitive)
+ Added@octokit/core@3.6.0(transitive)
+ Added@octokit/endpoint@6.0.12(transitive)
+ Added@octokit/graphql@4.8.0(transitive)
+ Added@octokit/openapi-types@12.11.0(transitive)
+ Added@octokit/plugin-paginate-rest@2.21.3(transitive)
+ Added@octokit/plugin-request-log@1.0.4(transitive)
+ Added@octokit/plugin-rest-endpoint-methods@5.16.2(transitive)
+ Added@octokit/request@5.6.3(transitive)
+ Added@octokit/request-error@2.1.0(transitive)
+ Added@octokit/rest@18.12.0(transitive)
+ Added@octokit/types@6.41.0(transitive)
+ Added@sideway/address@4.1.5(transitive)
+ Added@sideway/formula@3.0.1(transitive)
+ Added@sideway/pinpoint@2.0.0(transitive)
+ Added@stackbit/sdk@0.2.39(transitive)
+ Added@stackbit/utils@0.2.42(transitive)
+ Addedacorn@8.14.0(transitive)
+ Addedansi-regex@5.0.1(transitive)
+ Addedargparse@2.0.1(transitive)
+ Addedbefore-after-hook@2.2.3(transitive)
+ Addeddeprecation@2.3.1(transitive)
+ Addedesbuild@0.14.54(transitive)
+ Addedesbuild-android-64@0.14.54(transitive)
+ Addedesbuild-android-arm64@0.14.54(transitive)
+ Addedesbuild-darwin-64@0.14.54(transitive)
+ Addedesbuild-darwin-arm64@0.14.54(transitive)
+ Addedesbuild-freebsd-64@0.14.54(transitive)
+ Addedesbuild-freebsd-arm64@0.14.54(transitive)
+ Addedesbuild-linux-32@0.14.54(transitive)
+ Addedesbuild-linux-64@0.14.54(transitive)
+ Addedesbuild-linux-arm@0.14.54(transitive)
+ Addedesbuild-linux-arm64@0.14.54(transitive)
+ Addedesbuild-linux-mips64le@0.14.54(transitive)
+ Addedesbuild-linux-ppc64le@0.14.54(transitive)
+ Addedesbuild-linux-riscv64@0.14.54(transitive)
+ Addedesbuild-linux-s390x@0.14.54(transitive)
+ Addedesbuild-netbsd-64@0.14.54(transitive)
+ Addedesbuild-openbsd-64@0.14.54(transitive)
+ Addedesbuild-sunos-64@0.14.54(transitive)
+ Addedesbuild-windows-32@0.14.54(transitive)
+ Addedesbuild-windows-64@0.14.54(transitive)
+ Addedesbuild-windows-arm64@0.14.54(transitive)
+ Addedfs-extra@10.1.0(transitive)
+ Addedis-plain-object@5.0.0(transitive)
+ Addedjoi@17.13.3(transitive)
+ Addedjs-yaml@4.1.0(transitive)
+ Addedmoment@2.30.1(transitive)
+ Addednode-fetch@2.7.0(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedsemver@7.6.3(transitive)
+ Addedstrip-ansi@6.0.1(transitive)
+ Addedtr46@0.0.3(transitive)
+ Addeduniversal-user-agent@6.0.1(transitive)
+ Addedwebidl-conversions@3.0.1(transitive)
+ Addedwhatwg-url@5.0.0(transitive)
+ Addedwrappy@1.0.2(transitive)
- Removed@stackbit/schema@^0.1.1
- Removed@stackbit/schema@0.1.5(transitive)
Updatedchokidar@^3.5.2
Updatedfs-extra@^10.0.0
Updatedlodash@^4.17.21