import { CallExpressionShape, isSNScope, Plugin } from '@servicenow/sdk-build-core' import { NowIdShape } from './now-id-plugin' import { NowIncludeShape } from './now-include-plugin' const nameRegex = /^[a-zA-Z_$][0-9a-zA-Z_$]+$/ export const ScriptIncludePlugin = Plugin.create({ name: `ScriptIncludePlugin`, records: { sys_script_include: { async toShape(record, { transform }) { const script = await NowIncludeShape.fromRecord(record, record.get('script'), transform) return { success: true, value: new CallExpressionShape({ source: record, callee: 'ScriptInclude', args: [ record.transform(({ $ }) => ({ $id: $.val(NowIdShape.from(record)), name: $, script: $.val(script), description: $.def(''), apiName: $.from('api_name').def(''), callerAccess: $.from('caller_access').map((v) => v .ifString() ?.pipe((v) => v.equals('1') ? 'tracking' : v.equals('2') ? 'restriction' : undefined ) ), clientCallable: $.from('client_callable').toBoolean(), mobileCallable: $.from('mobile_callable').toBoolean(), sandboxCallable: $.from('sandbox_callable').toBoolean(), accessibleFrom: $.from('access').def('package_private'), active: $.toBoolean(), })), ], }), } }, }, }, shapes: [ { shape: CallExpressionShape, fileTypes: ['fluent'], async toRecord(shape, { factory, config, diagnostics }) { if (shape.getCallee() !== 'ScriptInclude') { return { success: false } } const prop = shape.getArgument(0).asObject() const name = prop.get('name').asString() if (!nameRegex.test(name.getValue())) { diagnostics.warn( name.getOriginalNode(), `Script include name '${name.getValue()}' is not a valid JavaScript identifier. This may cause issues when invoking the script include on the platform.` ) } const callerAccess = prop.get('callerAccess') if (callerAccess.isDefined() && prop.get('accessibleFrom').ifString()?.getValue() !== 'public') { diagnostics.hint( callerAccess.getOriginalNode(), `'callerAccess' is only effective when 'accessibleFrom' is set to 'public'. It is ignored by the platform otherwise.` ) } const apiName = prop.get('apiName') if (apiName.isDefined()) { const scope = config.scope if (!isSNScope(scope) && !apiName.asString().getValue().startsWith(`${scope}.`)) { diagnostics.error(apiName.getOriginalNode(), `apiName must begin with '${scope}.'`) } } return { success: true, value: await factory.createRecord({ source: shape, table: 'sys_script_include', explicitId: prop.get('$id'), properties: prop.transform(({ $ }) => { return { name: $, script: $.toCdata(), description: $, api_name: $.from('apiName').def(`${config.scope}.${name.getValue()}`), caller_access: $.from('callerAccess') .map((v) => { if (v.isUndefined()) { return undefined } const t = v.asString().getValue() as 'restriction' | 'tracking' switch (t) { case 'restriction': return 2 case 'tracking': return 1 default: return undefined } }) .def(''), client_callable: $.from('clientCallable').def(false), mobile_callable: $.from('mobileCallable').def(false), sandbox_callable: $.from('sandboxCallable').def(false), access: $.from('accessibleFrom').def('package_private'), active: $.def(true), sys_name: $.from('name'), } }), }), } }, }, ], })