Skip to content

Commit

Permalink
650 refactor imports (#668)
Browse files Browse the repository at this point in the history
  • Loading branch information
rolfheij-sil authored Dec 7, 2023
2 parents 8e6028c + 5e65e1c commit 278093e
Show file tree
Hide file tree
Showing 35 changed files with 1,456 additions and 1,358 deletions.
11 changes: 11 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ module.exports = {
'@typescript-eslint/no-explicit-any': 'error',
'no-redeclare': 'off',
'@typescript-eslint/no-redeclare': 'error',
'no-restricted-imports': [
'error',
{
patterns: [
{
group: ['shared/*', 'renderer/*', 'extension-host/*', 'node/*', 'client/*', 'main/*'],
message: `Importing from this path is not allowed. Try importing from @papi/core. Imports from paths like 'shared', 'renderer', 'node', 'client' and 'main' are not allowed to prevent unnecessary import break.`,
},
],
},
],
'no-shadow': 'off',
'@typescript-eslint/no-shadow': 'error',
'no-use-before-define': 'off',
Expand Down
13 changes: 12 additions & 1 deletion extensions/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
module.exports = {
extends: '../.eslintrc.js',
rules: {
'import/no-unresolved': ['error', { ignore: ['papi'] }],
'import/no-unresolved': ['error', { ignore: ['@papi'] }],
'no-restricted-imports': [
'error',
{
patterns: [
{
group: ['shared/*', 'renderer/*', 'extension-host/*', 'node/*', 'client/*', 'main/*'],
message: `Importing from this path is not allowed. Try importing from @papi/core. Imports from paths like 'shared', 'renderer', 'node', 'client' and 'main' are not allowed to prevent unnecessary import break.`,
},
],
},
],
},
overrides: [
{
Expand Down
3 changes: 1 addition & 2 deletions extensions/src/c-sharp-provider-test/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
declare module 'c-sharp-provider-test' {
import type { DataProviderDataType } from 'shared/models/data-provider.model';
import type IDataProvider from 'shared/models/data-provider.interface';
import { IDataProvider, DataProviderDataType } from '@papi/core';

type TimeDataTypes = {
Time: DataProviderDataType<undefined, string | undefined, never>;
Expand Down
2 changes: 1 addition & 1 deletion extensions/src/evil/evil.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

'use strict';

const papi = require('papi-backend');
const papi = require('@papi/backend');

const { logger } = papi;

Expand Down
3 changes: 1 addition & 2 deletions extensions/src/hello-someone/hello-someone.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
declare module 'hello-someone' {
import type IDataProvider from 'shared/models/data-provider.interface';
import type { DataProviderDataType } from 'shared/models/data-provider.model';
import { IDataProvider, DataProviderDataType } from '@papi/core';

export type Person = {
greeting?: string;
Expand Down
18 changes: 9 additions & 9 deletions extensions/src/hello-someone/hello-someone.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import papi, { logger } from 'papi-backend';
import type { ExecutionActivationContext } from 'extension-host/extension-types/extension-activation-context.model';
import type {
import papi, { logger } from '@papi/backend';
import {
DataProviderUpdateInstructions,
ExecutionActivationContext,
IWebViewProvider,
SavedWebViewDefinition,
WebViewContentType,
WebViewDefinition,
SavedWebViewDefinition,
} from 'shared/models/web-view.model';
WithNotifyUpdate,
IDataProviderEngine,
} from '@papi/core';
import type { PeopleData, PeopleDataMethods, PeopleDataTypes, Person } from 'hello-someone';
import type { DataProviderUpdateInstructions } from 'shared/models/data-provider.model';
import type IDataProviderEngine from 'shared/models/data-provider-engine.model';
import type { WithNotifyUpdate } from 'shared/models/data-provider-engine.model';
import type { IWebViewProvider } from 'shared/models/web-view-provider.model';
import helloSomeoneHtmlWebView from './hello-someone.web-view.html?inline';

logger.info('Hello Someone is importing!');
Expand Down
4 changes: 2 additions & 2 deletions extensions/src/hello-someone/hello-someone.web-view.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
<br />
<div><button id="new-web-view-button" type="button">Create a new People WebView!</button></div>
<script>
const papi = require('papi-frontend');
const { logger } = require('papi-frontend');
const papi = require('@papi/frontend');
const { logger } = require('@papi/frontend');

function print(input) {
logger.info(input);
Expand Down
3 changes: 1 addition & 2 deletions extensions/src/hello-world/hello-world.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { DataProviderDataType } from 'shared/models/data-provider.model';
import type { MandatoryProjectDataType } from 'shared/models/project-data-provider.model';
import type { DataProviderDataType, MandatoryProjectDataType } from '@papi/core';

declare module 'hello-world' {
/** Event containing information about `helloWorld` */
Expand Down
10 changes: 5 additions & 5 deletions extensions/src/hello-world/hello-world.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import papi, { logger } from 'papi-backend';
import type { ExecutionActivationContext } from 'extension-host/extension-types/extension-activation-context.model';
import papi, { logger } from '@papi/backend';
import type {
ExecutionActivationContext,
WebViewContentType,
WebViewDefinition,
SavedWebViewDefinition,
} from 'shared/models/web-view.model';
import type { IWebViewProvider } from 'shared/models/web-view-provider.model';
import type PapiEventEmitter from 'shared/models/papi-event-emitter.model';
IWebViewProvider,
PapiEventEmitter,
} from '@papi/core';
import type { HelloWorldEvent } from 'hello-world';
import helloWorldReactWebView from './web-views/hello-world.web-view?inline';
import helloWorldReactWebViewStyles from './web-views/hello-world.web-view.scss?inline';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useData } from 'papi-frontend/react';
import { useData } from '@papi/frontend/react';

export default function Clock() {
const [currentTime] = useData('current-time').Time(undefined, 'Loading current time');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import papi from 'papi-frontend';
import { useEvent } from 'papi-frontend/react';
import papi from '@papi/frontend';
import { useEvent } from '@papi/frontend/react';
import { useCallback, useState } from 'react';
import { Button } from 'papi-components';
import type { HelloWorldEvent } from 'hello-world';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
<script>
// Test React component to render in the root
const { createElement, useCallback, useState } = require('react');
const { usePromise, useEvent } = require('papi-frontend/react');
const papi = require('papi-frontend');
const { logger } = require('papi-frontend');
const { usePromise, useEvent } = require('@papi/frontend/react');
const papi = require('@papi/frontend');
const { logger } = require('@papi/frontend');

const NAME = 'Hello World React WebView in HTML';

Expand Down
7 changes: 3 additions & 4 deletions extensions/src/hello-world/web-views/hello-world.web-view.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ScrVers, VerseRef } from '@sillsdev/scripture';
import papi, { logger } from 'papi-frontend';
import papi, { logger } from '@papi/frontend';
import {
useData,
useProjectData,
Expand All @@ -8,7 +8,7 @@ import {
useSetting,
useDialogCallback,
useDataProvider,
} from 'papi-frontend/react';
} from '@papi/frontend/react';
import {
Button,
Checkbox,
Expand All @@ -20,10 +20,9 @@ import {
Table,
ScriptureReference,
} from 'papi-components';
import type { DialogTypes, WebViewProps } from '@papi/core';
import { Key, useCallback, useEffect, useMemo, useRef, useState } from 'react';
import type { HelloWorldEvent } from 'hello-world';
import type { DialogTypes } from 'renderer/components/dialogs/dialog-definition.model';
import type { WebViewProps } from 'shared/models/web-view.model';
import Clock from './components/clock.component';
import Logo from '../assets/offline.svg';

Expand Down
8 changes: 4 additions & 4 deletions extensions/src/project-notes-data-provider/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { VerseRef } from '@sillsdev/scripture';
import type {
DataProviderDataType,
DataProviderSubscriberOptions,
} from 'shared/models/data-provider.model';
import type IDataProvider from 'shared/models/data-provider.interface';
import type { PapiEvent } from 'shared/models/papi-event.model';
import type { Unsubscriber } from 'shared/utils/papi-util';
PapiEvent,
Unsubscriber,
IDataProvider,
} from '@papi/core';

declare module 'project-notes-data-provider' {
export type ProjectNotesProviderDataTypes = {
Expand Down
3 changes: 1 addition & 2 deletions extensions/src/quick-verse/quick-verse.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
declare module 'quick-verse' {
import type { DataProviderDataType } from 'shared/models/data-provider.model';
import type IDataProvider from 'shared/models/data-provider.interface';
import { DataProviderDataType, IDataProvider } from '@papi/core';

export type QuickVerseSetData = string | { text: string; isHeresy: boolean };

Expand Down
12 changes: 7 additions & 5 deletions extensions/src/quick-verse/quick-verse.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { VerseRef } from '@sillsdev/scripture';
import papi, { logger, DataProviderEngine } from 'papi-backend';
import type { ExecutionActivationContext } from 'extension-host/extension-types/extension-activation-context.model';
import type { ExecutionToken } from 'node/models/execution-token.model';
import type IDataProviderEngine from 'shared/models/data-provider-engine.model';
import papi, { logger, DataProviderEngine } from '@papi/backend';
import type {
ExecutionActivationContext,
ExecutionToken,
DataProviderUpdateInstructions,
IDataProviderEngine,
} from '@papi/core';
import type { QuickVerseDataTypes, QuickVerseSetData } from 'quick-verse';
import type { DataProviderUpdateInstructions } from 'shared/models/data-provider.model';

logger.info('Quick Verse is importing!');

Expand Down
10 changes: 5 additions & 5 deletions extensions/src/resource-viewer/resource-viewer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import papi, { logger } from 'papi-backend';
import type { IWebViewProvider } from 'shared/models/web-view-provider.model';
import type { DialogOptions } from 'shared/models/dialog-options.model';
import papi, { logger } from '@papi/backend';
import type {
DialogOptions,
ExecutionActivationContext,
GetWebViewOptions,
IWebViewProvider,
SavedWebViewDefinition,
WebViewDefinition,
} from 'shared/models/web-view.model';
import type { ExecutionActivationContext } from 'extension-host/extension-types/extension-activation-context.model';
} from '@papi/core';
import resourceViewerWebView from './resource-viewer.web-view?inline';
import resourceViewerWebViewStyles from './resource-viewer.web-view.scss?inline';

Expand Down
6 changes: 3 additions & 3 deletions extensions/src/resource-viewer/resource-viewer.web-view.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { VerseRef } from '@sillsdev/scripture';
import { logger } from 'papi-frontend';
import { useProjectData, useSetting } from 'papi-frontend/react';
import { logger } from '@papi/frontend';
import { useProjectData, useSetting } from '@papi/frontend/react';
import { ScriptureReference } from 'papi-components';
import { JSX, useMemo } from 'react';
import UsxEditor from 'usxeditor';
import type { WebViewProps } from 'shared/models/web-view.model';
import type { WebViewProps } from '@papi/core';

/** Characteristics of a marker style */
interface StyleInfo {
Expand Down
8 changes: 3 additions & 5 deletions extensions/src/usfm-data-provider/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ declare module 'usfm-data-provider' {
DataProviderDataType,
DataProviderSubscriberOptions,
DataProviderUpdateInstructions,
} from 'shared/models/data-provider.model';
import type IDataProvider from 'shared/models/data-provider.interface';
import type {
ExtensionDataScope,
IDataProvider,
MandatoryProjectDataType,
} from 'shared/models/project-data-provider.model';
import type { Unsubscriber } from 'shared/utils/papi-util';
Unsubscriber,
} from '@papi/core';

export type UsfmProviderDataTypes = {
BookNames: DataProviderDataType<boolean, string[], never>;
Expand Down
7 changes: 4 additions & 3 deletions extensions/webpack/webpack.config.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ const configBase: webpack.Configuration = {
'react/jsx-runtime',
'react-dom',
'react-dom/client',
'papi-frontend',
'papi-frontend/react',
'papi-backend',
'@papi/backend',
'@papi/core',
'@papi/frontend',
'@papi/frontend/react',
'@sillsdev/scripture',
],
module: {
Expand Down
3 changes: 2 additions & 1 deletion lib/papi-dts/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ module.exports = {
'import/newline-after-import': 'off',
// Ignore problems with pseudomodules provided in the src/declarations folder
// For example, papi-shared-types. If this causes problems, we can improve it
'import/no-unresolved': ['error', { ignore: ['papi-*'] }],
'import/no-unresolved': ['error', { ignore: ['papi-*', '@papi*'] }],
'vars-on-top': 'off',
'no-restricted-imports': 'off',
'no-var': 'off',
'no-undef': 'off',
// Stop warnings from JSDocs not being formatted with 'prettier-plugin-jsdoc' since it is
Expand Down
9 changes: 5 additions & 4 deletions lib/papi-dts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ This is a types package that provides type declarations for `papi` and other imp

The documentation includes a variety of types to support different aspects of the project. For extension development, the most crucial sections to focus on are:

- `@papi/frontend`: Papi objects that can be accessed in the frontend of the extension.
- `@papi/frontend/react`: React hooks to help interaction between React components and the Papi.
- `@papi/backend`: Papi objects that can be accessed in the backend of the extension.
- `@papi/shared`: Declarations shared between core and extensions
- `@papi/backend`: PAPI objects that can be accessed in the backend of the extension.
- `@papi/core`: Types for use when interacting with the PAPI.
- `@papi/frontend`: PAPI objects that can be accessed in the frontend of the extension.
- `@papi/frontend/react`: React hooks to help interaction between React components and the PAPI.
- `papi-shared-types`: Extensible interface declarations shared between core and extensions to expose extension API types on the PAPI

Feel free to explore other sections based on your specific needs and context.

Expand Down
20 changes: 12 additions & 8 deletions lib/papi-dts/edit-papi-d-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,23 @@ const PAPI_DTS_PATH = 'papi.d.ts';
// Load the papi.d.ts file for editing
let papiDTS = fs.readFileSync(PAPI_DTS_PATH, 'utf8');

// Rename papi modules to 'papi-whatever' so extensions can import just 'papi-whatever'
// Rename papi modules to '@papi/whatever' so extensions can import just '@papi/whatever'
papiDTS = papiDTS
.replace(
new RegExp(escapeStringRegexp("'shared/services/papi-core.service'"), 'g'),
"'@papi/core'",
)
.replace(
new RegExp(escapeStringRegexp("'renderer/services/papi-frontend.service'"), 'g'),
"'papi-frontend'",
"'@papi/frontend'",
)
.replace(
new RegExp(escapeStringRegexp("'renderer/services/papi-frontend-react.service'"), 'g'),
"'papi-frontend/react'",
"'@papi/frontend/react'",
)
.replace(
new RegExp(escapeStringRegexp("'extension-host/services/papi-backend.service'"), 'g'),
"'papi-backend'",
"'@papi/backend'",
);

// #region Copy "JSDOC DESTINATION" blocks to "JSDOC SOURCE" blocks
Expand Down Expand Up @@ -100,12 +104,12 @@ papiDTS = papiDTS.replace(
// Add ignore error messages to the `useData` and `useProjectData` signatures where TS pretends like
// it doesn't know how to index the types but it works just fine
papiDTS = papiDTS.replace(
/(?<!\/\/ @ts-expect-error.*)(\n(\s*).*DataProviderTypes\[DataProviderName\]\[TDataType\]\['(\w+)'\])/g,
"\n$2// @ts-expect-error TypeScript pretends it can't find `$3`, but it works just fine$1",
/(?<!\/\/ @ts-ignore.*)(\n(\s*).*DataProviderTypes\[DataProviderName\]\[TDataType\]\['(\w+)'\])/g,
"\n$2// @ts-ignore TypeScript pretends it can't find `$3`, but it works just fine$1",
);
papiDTS = papiDTS.replace(
/(?<!\/\/ @ts-expect-error.*)(\n(\s*).*ProjectDataTypes\[ProjectType\]\[TDataType\]\['(\w+)'\])/g,
"\n$2// @ts-expect-error TypeScript pretends it can't find `$3`, but it works just fine$1",
/(?<!\/\/ @ts-ignore.*)(\n(\s*).*ProjectDataTypes\[ProjectType\]\[TDataType\]\['(\w+)'\])/g,
"\n$2// @ts-ignore TypeScript pretends it can't find `$3`, but it works just fine$1",
);

// Fix all the path-aliased imports. For some reason, generating `papi.d.ts` removes the @ from path
Expand Down
Loading

0 comments on commit 278093e

Please sign in to comment.